I have to set up an array of strings dynamically that I will read words in from a dictionary file (dictionary.txt). I know that the maximum length of letters for any given word is 19 letters. Also, maxWord will be given the value of the total number of words in the dictionary.txt file that I will be reading into memory.
Code:
#include <stdio.h>
#include <stdlib.h>
char **dictionary;
const MAX_LENGTH = 19;
int maxWord;
int i;
FILE *ifp = fopen("dictionary.txt", "r"); // open dictionary file
fscanf (ifp, "%d", &maxWord); // read number of words in dictionary
dictionary = malloc(maxWord * sizeof(char*)); // allocate memory
for (i = 0; i < maxWord; i++)
dictionary[i] = malloc((MAX_LENGTH) * sizeof(char));
for (i = 0; i < maxWord; i++) // read dictionary words into dictionary array
{
fscanf (ifp, "%s", dictionary[i]);
printf ("%s\n", dictionary[i]); // temp. confirms dictionary array working
}
fclose(ifp); // close dictionary.txt
}
My program compiles, and I confirmed with the printf statements that the array is working, but I get a program error when I get to a function that I send the dictionary array to. I send it in the following format.
Code:
myFunction (dictionary, otherVar, otherVar2);
void myFunction (char **dict[][MAX_LENGTH], int var, int var2) {
blah blah blah
}