鱼C论坛

 找回密码
 立即注册
查看: 2897|回复: 5

这段代码为什么执行到最后不输出,就退出了?

[复制链接]
发表于 2011-8-10 18:17:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<malloc.h>
#define NULL ((void *)0)
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p1=p2;
p1=(struct student*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}
main()
{
creat();
void print(struct student *head);
system("pause");

}
是主函数调用的问题吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-10 19:21:33 | 显示全部楼层
第三行#define NULL ??????
NULL可以定义吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-8-10 20:02:55 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-20 20:15:09 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

//#define NULL ((void *)0)

#define LEN sizeof(struct student)

struct student

{
long num;
float score;
struct student *next;
};

int n;

struct student *creat(void)
{

struct student *head;

struct student *p1,*p2;

n=0;

p1=p2=(struct student*)malloc(LEN);

scanf("%ld,%f",&p1->num,&p1->score);

head=NULL;

while(p1->num!=0)

{

n++;//n++的效率会高一点儿吧if(n==1)

head=p1;

else

p2->next=p1;

p1=p2;

p1=(struct student*)malloc(LEN);

scanf("%ld,%f",&p1->num,&p1->score);

}

p2->next=NULL;

return(head);

}

void print(struct student *head)

{

struct student *p;

printf("\nNow,These %d records are:\n",n);

p=head;

if(head!=NULL)
{
do

{

printf("%ld %5.1f\n",p->num,p->score);

p=p->next;

}

while(p!=NULL);

}
}

main()

{
        struct student *head;
        void print(struct student *head);//这里是函数声明不是调用        head=creat();

        print(head);
        system("pause");


}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-20 20:16:40 | 显示全部楼层
不好意思,那个代码错乱了,但是我是想说明问题的所以特地用了红颜色的,你把那个head=creat()自己移下来吧,好看点儿!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-8-20 20:18:33 | 显示全部楼层
响当当 发表于 2011-8-20 20:15
#include
#include
#include

灰常感谢,这个问题已经解决了
你指出的问题
我已经改正了
尽管这样
还是很
感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-8 19:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表