看看这个小程序错在哪里?

来源:百度知道 编辑:UC知道 时间:2024/06/08 10:10:29
这段代码运行输入05040001 zhang 270之后输出却不对
(学号为0时跳出输入循环)
#include<malloc.h>
#include<stdio.h>
#define LEN sizeof(struct student)
#define NULL 0
struct student/*定义结构体变量*/
{
char Num[8];
char Name[16];
float score;
struct student *next;
};

int n;

struct student *creat(void)/*定义函数创建链表,返回一个指向链表表头的指针*/
{
char cmp[]={"000"};
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%s",p1->Num);
scanf("%s",p1->Name);
scanf("%f",&p1->score);
head=NULL;
while(strcmp(p1->Num,cmp)>0)/*当学号不为0时*/
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%s",p1->Num);
scanf("%s",p1->Name);

首先你定义结构体中的序号的大小有问题
char Num[8];
你改成char Num[9],试一下!
因为你采用了如下输入方式,
scanf("%s",p1->Num);
字符串"05040001"在内存中是{05040001\0}占9个字符长度。

``````很长的“小程序”,粗略看了一下,主函数中的打印应该是“printf”,而不是“print”。