Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22016

This piece of code is giving segmentation fault [closed]

$
0
0

Why is this code giving segmentation fault after printf("9\n"). Basically the code crashes as soon as the while loop starts. Can anyone tell me how to fix it? Consider I called the headrvLoader()inmain()```

tetypedef struct cartbook{  int issue;  char *id;  struct cartbook *next;}cartstruct;struct cartbook *headrv;void headrvLoader(){  FILE *fp1,*fp2;printf("1\n");  struct cartbook *current,*prev,*p;printf("2\n");  fp1=fopen("ReservedBooks.txt","r");printf("3\n");  fp2=fopen("ReservedUserId.txt","r");printf("4\n");  headrv=(struct cartbook*)malloc(sizeof(struct cartbook));printf("5\n");  char *str;printf("6\n");  current=(struct cartbook*)malloc(sizeof(struct cartbook));printf("7\n");  headrv->next=current;printf("8\n");  prev=headrv;printf("9\n");  while(true)  {    if(fgets(current->id,50,fp2)==NULL)    {        printf("jhhj");      current=NULL;      prev->next=NULL;    }}}

Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>