如图所示,蓝色区域的表达式q2->next=q;出现了问题,程序已一运行到这步就会停止运行,该两个指针都是user型,已声明typedef struct USER *user。求帮助
解决方案
30
根据你的代码构建了一个运行环境,读数据是没有问题的,你看一下吧
#include "stdio.h" #include "string.h" #include "stdlib.h" typedef struct USER { int number; char name[20]; char code[20]; int itema; struct ITEM* a; struct USER* next; }*user; typedef struct LinkList { int amount; struct USER* next; }*userlist; typedef int Status; Status InitList(userlist *p) { user q; *p=(userlist)malloc(sizeof(struct LinkList)); (*p)->amount=0; q=(user)malloc(sizeof(struct USER)); q->number=0; q->name[0]="a"; q->name[1]="d"; q->name[2]="m"; q->name[3]="i"; q->name[4]="n"; q->name[5]="\0"; q->code[0]="a"; q->code[1]="d"; q->code[2]="m"; q->code[3]="i"; q->code[4]="n"; q->code[5]="\0"; q->a=NULL; q->next=NULL; if(!p) /* 存储分配失败 */ exit(0); (*p)->next=q; return 1; } int ReadFile(userlist p) { FILE *fp; user q,q2; struct ITEM *n,*n2; int i,j; q2=p->next; if((fp=fopen("memory.txt","r"))!=NULL) { fscanf(fp,"%d,",&(p->amount)); printf("%d",p->amount); for(i=1;i<=p->amount;i++) { q=(user)malloc(sizeof(struct USER)); fscanf(fp,"%d%20s%20s%d,",&(q->number),&(q->name),&(q->code),&(q->itema)); printf("%d,%s,%s,%d,",(q->number),(q->name),(q->code),(q->itema)); q2->next=q; n2=q->a; /* for(j=1;j<=q->itema;j++) { n=(item)malloc(sizeof(struct ITEM)); fscanf(fp,"%d%20s%d,%d,%d,%d,%d,",&(n->number),&(n->name),&(n->amount),&(n->year),&(n->month),&(n->day),&(n->sort)); n2->next=n; n2=n; } n2->next=NULL;*/ } fclose(fp); } return 0; } int main() { int px,py; int result; userlist a; char c; //CreateThread( NULL,0,ThreadProc1,NULL,0, NULL); InitList(&a); ReadFile(a); system("pause"); return 0; }