The goal is to read in 105 words in from a file and assign them to an array. The array is initialized to
char **array = (char**)malloc(*capacity * sizeof(char*));
where capacity is set to 50.
I'm trying to make it so this function is called when 50 words have been read in and the 51st is being ready to. The initial allocation for the array should be space for 50 pointers. If the array ever gets full (i.e., wordCount equals current capacity), and there's a word to insert, I will double the capacity of the array (i.e., malloc a new array of twice the current size, copy the strings over into the front half of the newly allocated array, and free the space used by the old array. I am not allowed to use realloc. I would just like help fixing the way to do this using my method. Please do not suggest alternate methods.
So I keep getting a segmentation fault and I don't understand why. Any advice?
int doubleArraySize(char ***array, int count, int *capacity){
char **temp = (char**)malloc(*capacity * sizeof(char*));
if (temp = NULL){
return -1;
}
int i = 0;
while(i<count){
temp[i] = *array[i];
i++;
}
i = 0;
while(i<count){
free(*array[i]);
i++;
}
free(*array);
*array = temp;
return 0;
}
And yes I am not allowed to use realloc. I was specifically told to create a new array of double the size and copy the elements to the first half then free the old one. Also in a debugger i am using, it says the problem lies in the line
temp[i] = *array[i];
but i have no idea why. Thank you in advanced!