Segmentation fault (core dumped)

Post here if you don't find any other place for your post. But please, stay on-topic: algorithms, programming or something related to this web site and its services.

Moderator: Board moderators

Post Reply
henrique.renno
New poster
Posts: 6
Joined: Tue Dec 27, 2005 6:13 pm

Segmentation fault (core dumped)

Post 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);
}
misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post 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.
Post Reply

Return to “Other words”