I'm having big problems while creating a simple program in C.
I have the following function:
void createStrings (char *dictionary[], int *n) {
int i;
char word[20];
printf ("Insert how many words to use: ");
scanf ("%d", &(*n));
// Initialization
for (i = 0; i < *n; i++) {
dictionary[i] = '\0';
}
// Populating the array with words
for (i = 0; i < *n; i++) {
printf ("Insert the word in position %d: ", i);
scanf("%s", word);
dictionary[i] = word;
}
}
In the main I read the array of words just populated by using
printf ("Following words have been inserted:\n");
for (i = 0; i < n; i++) {
printf ("dictionary[%d] = %s\n", i, dictionary[i]);
}
I'm pretty sure this last for cycle has been implemented well.
When I run the program and try to insert, for example, three different words like "one" "two" and "zero" I get the following output:
Insert how many words to use: 3
Insert word number 0: One
Insert word number 1: Two
Insert word number 2: Zero
Following words have been inserted:
dictionary[0] = Zero
dictionary[1] = Zero
dictionary[2] = Zero
It's like only the last word I inserted gets saved and the cycle goes all the way back to override all the other elements of the array.
Any help would be greatly appreciated.