Array of strings overwriting each other?
I am making a function that turns a list of words into an array to be used by other functions, but somehow I'm overwriting previous words. I check the memory address' and they seem different, but when I recheck once I'm done importing the words, they are all the same.
static char **array;
//takes the name of a data file and reads it into an array
static void InitDictionary(char *fileName){
//slide 36, chap 3
FILE *file;
int count,i;
char dummy[30];
file = fopen(fileName, "r");
while( fscanf(file, "%s", dummy) == 1 ){//counting at first
count++;
}
fclose(file);
array = (char**) malloc(count * sizeof(char*) );
count = 0;
file = fopen(fileName, "r");
while( fscanf(file, "%s", dummy) == 1 ){//now putting values in array
char newEntry[30];
strcpy(newEntry,dummy);
array[count] = newEntr开发者_开发问答y;
printf("%d - %s : %p \n",count, array[count], &array[count]);
count++;
}
fclose(file);
for(i=0;i<count;i++)
printf("%d - %s : %p\n",i, array[i], &array[count] );
}
Thanks
You need to allocate new memory for newEntry
each time through the while loop. You're currently storing a pointer to the single newEntry
buffer multiple times.
When you said you've checked the addresses, what address did you check specifically?
And actually, what's probably technically happening here is that you're storing references to a variable that goes out of scope after each iteration of the while loop. Since it is out of scope, the compiler is then free to reuse that stack memory which it does for the next iteration of the loop.
user470379 is correct, you aren't allocating space for each new word. One possible fix is to replace the three lines:
char newEntry[30];
strcpy(newEntry,dummy);
array[count] = newEntry;
with
array[count] = strdup(dummy);
One problem I see, count isn't initialized and you're using it in your malloc.
精彩评论