我这个程序不能运行 为什么?

[复制链接]
查看11 | 回复5 | 2010-5-29 12:37:19 | 显示全部楼层 |阅读模式
#include
#include
#define LEN sizeof (struct stu)
#define NULL 0
struct stu
{int num;
char xm[10];
char sex[2];
struct stu *next;
};
int n;


struct stu *creat()
{struct stu *head,*p1,*p2;
n=0;
p1=p2=(struct stu *)malloc(LEN);
scanf("%d,%s,%s,&p1->num,&p1->xm,&p1->sex");
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1
;else p2->next=p1;
p2=p1;
p1=(struct stu *)malloc(LEN);
scanf("%d,%s,%s,&p1->num,&p1->xm,&p1->sex");
}
p2->next=NULL;
return(head);}
main()
{
struct stu *p;

p=creat();
printf("%d%s%s",p->num,p->xm,p->sex);
}
新手就20分全给了多谢
双引号的错误是失误,那也不是关键

回复

使用道具 举报

千问 | 2010-5-29 12:37:19 | 显示全部楼层
修改如下:#include#include#define LEN sizeof (struct stu)#define NULL 0struct stu{int num; char xm[10]; char sex[2]; struct stu *next;};int n;
struct stu *creat(){struct stu *head,*p1,*p2;n=0;p1=p2=(struct stu *)malloc(LEN);scanf("%d,%[^,]%*c%s",&p1->num,&p1->xm,&p1->s
回复

使用道具 举报

千问 | 2010-5-29 12:37:19 | 显示全部楼层
两个scanf函数括号内的双引号
回复

使用道具 举报

千问 | 2010-5-29 12:37:19 | 显示全部楼层
scanf("%d,%s,%s,&p1->num,&p1->xm,&p1->sex");这一句,后边的双引号位置错了。。。应该是scanf("%d,%s,%s",&p1->num,&p1->xm,&p1->sex); 另外,scanf输入两个字符串是不能用逗号分隔的,应该用空格。因为如果用逗号分隔,它会被编译系统识别为要输入的字符串的一部
回复

使用道具 举报

千问 | 2010-5-29 12:37:19 | 显示全部楼层
#include#include#define LEN sizeof (struct stu) #define NULL 0struct stu{int num; char xm[10]; char sex[2]; struct stu *next;};int n;
回复

使用道具 举报

千问 | 2010-5-29 12:37:19 | 显示全部楼层
#include#include#define LEN sizeof (struct stu)#ifndef NULL#define NULL 0#endifstruct stu{int num; char xm[10]; char sex[2]; struct s
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行