Relatively new to C here. I have a program that reads in a "Dictionary" of words from a text file. Each word in the text file ends with a newline. I would like to dynamically allocate and store an array containing words of a certain length determined by the user. I am not sure that I completely understand how it works. My code doesn't seem to create this array and only stores the last string that it encounters.
It starts by counting the amount of words of the specified length, then stores that as the "rows" of the array.
My code so far:
int rows = 0, columns = letterAmount;
while(fgets(word, MAX_CHARS, dictionary)){
if(strlen(word) == letterAmount + 2){
rows++;
}
}
char **wordList = malloc(rows * sizeof(char *));
for(i = 0; i < rows; i++){
wordList[i] = (char *)malloc(columns + 1);
}
rewind(dictionary);
i = 0;
while(fgets(word, MAX_CHARS, dictionary) && i < rows){
if(strlen(word) == letterAmount + 2){
wordList[i] = word;
i++;
}
}
Any help is appreciated!