c语言 基础链表 意外中断。谢谢

(在输出位置意外中断,程序功能是输入姓名和学号,然后在首位再插入一个学生,然后输出整个链表,中断位置在输出函数的第一次姓名输出)

#include
#include

struct Student
{
char cName;
int iNumber;
struct Student *pNext;
};

int iCount;

struct Student *Create()
{
struct Student *pHead=NULL;
struct Student *pEnd,*pNew;
printf("Plase enter Name first,then Number:\n");
pEnd=pNew=malloc(sizeof(struct Student));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
while(pNew->iNumber!=0)
{
iCount++;
if(iCount==1)
{
pNew->pNext=NULL;
pEnd=pNew;
pHead=pNew;
}
else
{
pNew->pNext=NULL;
pEnd->pNext=pNew;
pEnd=pNew;
}
pNew=malloc(sizeof(struct Student));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
}
free(pNew);
return pHead;
};

void Print(struct Student *pHead)
{
struct Student *pTemp;
int iIndex=1;
printf("the list has %d members\n\n",iCount);

pTemp=pHead;

while(pTemp!=NULL)
{
printf("No.%d student:\n",iIndex);
printf("accomplished");
printf("Name:%s",pHead->cName);
printf("Number:%d\n",pTemp->iNumber);
pTemp=pTemp->pNext;
iIndex++;
}
};

struct Student *Insert(struct Student *pHead)
{
struct Student *pInsert;
printf("Insert member at first\n");
pInsert=malloc(sizeof(struct Student));
scanf("%s",&pInsert->cName);
scanf("%d",&pInsert->iNumber);
pInsert->pNext=pHead->pNext;
pHead->pNext=pInsert;
iCount++;
return pHead;

};

main()
{
struct Student *pHead;

printf("readng...\n");
pHead=Create();
printf("Insert readying\n");
pHead=Insert(pHead);
printf("\nInsert accomplished\n");
Print(pHead);

return 0;
}

1个回答

char cName;
->
char cName[100];

你只有1个字符,存不下name

caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复xuannfann: 问题解决的话,请点下采纳,谢谢了
大约一年之前 回复
xuannfann
xuannfann 我刚刚仔细从头查的时候才发现 之前一直纠结在Create和Print函数里 简直了 哈哈
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!