Page 1 of 1

Segmentation fault (core dumped)

Posted: Tue Dec 27, 2005 6:36 pm
by henrique.renno
Hello!
I've started writing a program that reads a number of people and get some data from a input file. After that, it tries to write these data in an output file but instead it gives me a "segmentation fault (core dumped)" error. I know it's due wrong access to memory but I don't know where is the flaw in my code. I'm using gcc.
Thanks!

#include <stdio.h>
#include <stdlib.h>

struct person
{
char name[14];
short int give, recv;
};

int main(void)
{
struct person *people;
FILE *fin, *fout;
short int np, i;

fin = fopen("gift1.in","r");
fout = fopen("gift1.out","w");

fscanf(fin, "%d", &np);
if((np < 2) || (np > 10))
exit(0);

people = (struct person *) malloc(np * sizeof(struct person));

for(i = 0;i < np;i++)
{
fscanf(fin, "%s", people.name);
people.give = 0;
people.recv = 0;
fprintf(fout, "%s %d %d\n", people.name, people.give, people.recv);
}

fclose(fin);
fclose(fout);
return(0);
}

Posted: Wed Dec 28, 2005 12:19 am
by misof
Please avoid cross-posting -- always post your message only once. It is considered impolite (as two people may spend their time answering your post) and most people tend to read most of the threads anyway.