#include <stdio.h>
#include <stdlib.h>
int main()
{
char *strs[6], array[100];
int i;
FILE *file = fopen("random.txt", "r");
for (i=0 ; i<6; i++)
{
fscanf(file ,"%s", array);
strs[i]= array;
printf("%s ", strs[i]);
}
printf("%s", strs[3]);
return 0;
}
Inside the random.txt:
one two three four five six
Output is: one two three four five six six
My question is why the last one 'six', I cannot access third element that is 'four'. Why doesn not record them in char array of pointers?
strs[i]= array;->strs[i]= strdup(array);If there is nostrdupusemalloc/memcpyto get the same. Here all the pointers point to array and that array containssixat last.