简体   繁体   中英

fscanf segfault reading into an array

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM