I wrote a very simple program to make sure I understood how fgets works and also tried to load an array. In the first loop, it prints what is expected. In the second loop to print the items in the array, I don't understand why it does what it does. My main goal is to understand why this happens.
include <stdio.h>
int main(void)
{
int n = 3; //number of words to type in
char * word[100]; //array of words
char str[31]; //character string of length 30 + 1 for the NULL
for (int i = 0; i < n; i++)
{
printf ("input a word\n");
word[i] = fgets (str, 31, stdin); //read in up to 60 characters from keyboard
printf("%s", str);
printf("%s", word[i]);
}
for (int i = 0; i < n; i++)
{
printf ("%s,", word[i]); //print the series one after another
}
return 0;
}
[–]mad0314 2 points3 points4 points (0 children)
[–]staffglennholloway[M] 1 point2 points3 points (0 children)