Why is fscanf in this code giving me a segfault?
int main(int argc, char *argv[])
{
FILE *file = fopen(argv[1], "r");
int n = atoi(argv[2]);
char *words[n]; int i=0;
while ((fscanf(file, "%s ", &words[i])) != EOF) i++;
}
Because you didn't allocate memory for your words[i]. They are just pointers point to random memory addresses.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.