I'm new user in stackoverflow. I wrote this code in c and I have no problem and the output is correct.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str[10];
FILE * fp;
fp = fopen ("file.txt", "w+");
fputs("We\nare\nin\n2016", fp);
rewind(fp);
fscanf(fp, "%s", str[0]);
fscanf(fp, "%s", str[1]);
printf("Read String1 |%s|\n", str[0] );
printf("Read String2 |%s|\n", str[1] );
fclose(fp);
return(0);
}
but when I use char *str[15] instead of char *str[10], the result is segmentation fault. What is wrong?
strarray point? Just because a program with undefined behavior seems to work doesn't mean it's correct.char *str[10], it's just pure luck it seems to work (good or bad luck is a question of perspective). It just seems to work, you will still overwrite some memory seemingly randomly. Remember, one of the possible symptoms of undefined behavior is that it actually works, unfortunately.