Elliot Gorokhovsky
Elliot Gorokhovsky

Reputation: 3762

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++;
}

Upvotes: 1

Views: 72

Answers (1)

Holsety
Holsety

Reputation: 103

Because you didn't allocate memory for your words[i]. They are just pointers point to random memory addresses.

Upvotes: 1

Related Questions