鱼C论坛

 找回密码
 立即注册
查看: 2572|回复: 4

C语音怎么这个不加也可以运行

[复制链接]
发表于 2018-3-19 23:52:07 | 显示全部楼层 |阅读模式

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

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

x
#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);
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-20 00:07:27 | 显示全部楼层
不知道你用的什么编译器不加还能运行。
最主要是
    p2->next=NULL;
        return head;
};

这样还能运行,真的很奇葩。
要不你重起电脑在试试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-20 08:10:36 | 显示全部楼层
新出的c编译器好像都智能化了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-20 08:55:20 | 显示全部楼层
#define只是负责复制粘贴
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-20 15:33:52 | 显示全部楼层
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;
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 05:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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