C语音怎么这个不加也可以运行
#define LEN sizeof(student)为什么这个没加struct也能运行??#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(student)
struct student *creat();
void print(struct student *head);
struct student
{
int num;
int score;
struct student *next;
};
int n;
int main()
{
struct student *stu;
stu=creat();
print(stu);
printf("\n");
system("pause");
return 0;
}
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("输入学好:");
scanf("%d",&p1->num);
printf("输入成绩:");
scanf("%d",&p1->score);
head=NULL;
n=0;
while(p1->num)
{
n++;
if(n==1)head=p1;
else p2=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("输入学好:");
scanf("%d",&p1->num);
printf("输入成绩:");
scanf("%d",&p1->score);
p2->next=p1;
}
p2->next=NULL;
return head;
};
void print(struct student *head)
{
struct student *p;
printf("\n这是%d个记录!\n\n",n);
p=head;
if(head)
{
do
{
printf("学号为%d的成绩是:%d\n",p->num,p->score);
p=p->next;
}while(p);
}
} 不知道你用的什么编译器不加还能运行。
最主要是
p2->next=NULL;
return head;
};
这样还能运行,真的很奇葩。
要不你重起电脑在试试。 新出的c编译器好像都智能化了 #define只是负责复制粘贴 alltolove 发表于 2018-3-20 08:10
新出的c编译器好像都智能化了
但是如果这个不加会报错
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(student)
struct student
{
int num;
int score;
struct student *next;
};
struct student *creat();
void print(struct student *head);
int n;
int main()
{
struct student *stu;
stu=creat();
print(stu);
system("pause");
return 0;
}
struct student *creat()
{
n=0;
struct student *head;
struct student *p1,*p2;
p2=p1=(struct student *)malloc(LEN);
head=NULL;
printf("请输入学生学号:");
scanf("%d",&p1->num);
printf("请输入成绩:");
scanf("%d",&p1->score);
if(p1->num)
{
head=p1;
while(p1->num)
{
p2=p1;
n++;
p1=(struct student *)malloc(LEN);
printf("请输入学生学号:");
scanf("%d",&p1->num);
printf("请输入成绩:");
scanf("%d",&p1->score);
p2->next=p1;
}
p2->next=NULL;
}
return head;
}
void print(struct student *head)
{
printf("共记录了%d条记录\n\n",n);
struct student *p;
p=head;
while(p)
{
printf("学号为:%d的同学的成绩为:%d",p->num,p->score);
p=p->next;
}
}
页:
[1]