I'm trying to make a sort-of-dictionary (don't think "hashmap" when you see that d-word, my dictionary is just simple lists of words in a 3D array)
e.g. dicts[7] is the struct dict for 7-letter words (7 excludes \0).
dicts[0] .. [2] will always be unused, and depending on the problem-instance, some (or many) other dicts[N] may also be unused.
After some preparatory work (examine problem-instance to learn necessary word-lengths, read words of necessary lengths from disk through a series of regex-like filters to get word-count for each word-length), I'm ready to malloc.
Imagine 123 words of 7-letters.
What I want to be able to do is iterate through those 7-letter words: i.e. use something like
to set/get the 6th 7-letter word with strncpy().
Here is just one of my 99 failed attempts:
...malloc did something, but certainly not what I wanted it to do :(
Tweaking around with variations **, *, & juggles segfaults/errors/warnings, but nothing I've tried has actually worked!
I've got my pointers in a twist: please help.
Chris
Code:
#define MAX_WORDLENGTH 20
struct dict{
int wordcount;
char **words;
};
struct dict dicts[MAX_WORDLENGTH + 1]; // +1 to get an index for MAX_WORDLENGTH
dicts[0] .. [2] will always be unused, and depending on the problem-instance, some (or many) other dicts[N] may also be unused.
After some preparatory work (examine problem-instance to learn necessary word-lengths, read words of necessary lengths from disk through a series of regex-like filters to get word-count for each word-length), I'm ready to malloc.
Imagine 123 words of 7-letters.
What I want to be able to do is iterate through those 7-letter words: i.e. use something like
Code:
dicts[7].words[5]
Here is just one of my 99 failed attempts:
Code:
printf(" .words: %p before malloc\n", dicts[wordlen].words);
// .words: 0x0 before malloc
int wordlen = 7;
char (*p)[wordlen+1]; // +1 for \0
dicts[wordlen].words = malloc(dicts[wordlen].wordcount * sizeof *p);
printf(" .words: %p after malloc\n", dicts[wordlen].words);
// .words: 0x14c606690 after malloc
printf(" .words[5]: %p after malloc\n", dicts[wordlen].words[5]);
// .words[5]: 0x0 after malloc
Tweaking around with variations **, *, & juggles segfaults/errors/warnings, but nothing I've tried has actually worked!
I've got my pointers in a twist: please help.
Chris
Comment