鱼C论坛

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

[已解决]单向链表

[复制链接]
发表于 2018-8-17 16:11:31 | 显示全部楼层 |阅读模式

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

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

x
书上的  单向动态链表程序     p1 p2定义成指向结构体struct student的指针   为什么scanf 里还需要加&(注释?的那行)   p1 p2不是已经是地址了么

#include<stdio.h>
#include<stdlib.h>

#define LEN sizeof(struct student)
struct student
{
        int num;                                            //学号      
        float score;                                        //成绩
        struct student *next;
};

int n;

struct student *creat()
{
        struct student *head;
        ]struct student *p1,*p2;                                  //指向struct student 的指针p1 p2

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

        scanf("%ld%f",&p1->num, &p2->score);           //???????????、

        head=NULL;
        n=0;
        while(p1->num!=0)
        {
                n=n+1;
                if(n==1)
                {
                        head=p1;
                }
                else
                {
                        p2->next=p1;
                        p2=p1;
                }
                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("you %d ge shu jv:\n",n);
        p=head;
        if(head!=NULL)
        {
                while(p!=NULL)
                {
                        printf("%ld %5.1f\n",p->num,p->score);
                        p=p->next;
                }
        }
}

void main()                                   //主
{
        struct student *head;
        head=creat();
        print(head);
}
最佳答案
2018-8-17 23:08:28
本帖最后由 claws0n 于 2018-8-18 10:53 编辑
HISIOISIH 发表于 2018-8-17 19:31
是不是应该从 运算符优先级 来看 & 取的是 p1->num 这个整体  而不是单取p1


更正:是的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-8-17 16:23:32 | 显示全部楼层
p 是指针,不是地址。也就是说 p 所存储的资料是 student 结构的地址,但它本身有自己的地址。 scanf 是把输入资料写入相对应的地址。基本上 scanf 要配合 &,就跟 int main() 配合 return 0 一样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-17 19:31:09 | 显示全部楼层
claws0n 发表于 2018-8-17 16:23
p 是指针,不是地址。也就是说 p 所存储的资料是 student 结构的地址,但它本身有自己的地址。 scanf 是把 ...

是不是应该从 运算符优先级 来看 & 取的是 p1->num 这个整体  而不是单取p1  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-17 23:08:28 | 显示全部楼层    本楼为最佳答案   
本帖最后由 claws0n 于 2018-8-18 10:53 编辑
HISIOISIH 发表于 2018-8-17 19:31
是不是应该从 运算符优先级 来看 & 取的是 p1->num 这个整体  而不是单取p1


更正:是的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-18 22:16:00 | 显示全部楼层

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 04:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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