#include <stdio.h>
#include <string.h>
typedef struct {
int count; // counter to use the word's occurrence
char *word;
} word;
int main() {
word myword[50];
FILE *myfile;
if ((myfile = fopen("wordlist", "r")) == NULL) {
printf("\n Error:Can't open file \n ");
exit(0);
}
char str[99];
int position = 0;
while (fscanf(myfile, "%s", str) != EOF) {
myword[position].word = str;
printf("myword[%d].word is: %s\n",position, myword[position].word);
position++;
if (position == 50) {
break;
}
}
printf("testing myword[0].word: %s\n", myword[0].word);
printf("testing myword[25].word: %s\n", myword[25].word);
printf("testing myword[49].word: %s\n", myword[49].word);
return 0;
}
Why do the last 3 printf lines return the same word: 50th word(myword[49].word)? The printf inside the while loop shows myword[position].word like it's supposed to.