-
segmentation fault
i have a data.raw file whose contents are
4
8 register
5 fine
3 afda
4 asd
all i was supposed to do is to copy the names into a structure array here is the code i have written which is showning segmentation fault on exectution
Code:
struct extract
{
int p_num;
int p_len;
char *data;
};
int main(int argc , char * argv[]){
FILE *fp;
int n,i;
struct extract *e;
fp=fopen("data.raw","r");
fscanf(fp,"%d",&n);
e=(struct extract*)malloc(n*sizeof(struct extract));
e[0].p_num=n;
for(i=1;i<=n;i++){
fscanf(fp,"%d %s",&e[i].p_len,e[i].data);
printf("%d %s\n",e[i].p_len,e[i].data);
}
return 0;
}
-
> for(i=1;i<=n;i++)
An array of n slots has 0 to n-1 as subscripts.
You're running off the end by 1 step.
-
You're also not allocating any space for each e[i].data either.
-
thanks salem..
it solved the problem