鱼C论坛

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

[已解决]*&head啥意思

[复制链接]
发表于 2020-12-14 21:17:03 | 显示全部楼层 |阅读模式

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

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

x
  1. void initialize(struct student *&head)
  2. {
  3.         head=(struct student*)malloc(LEN);
  4.         if(head==NULL)
  5.         {
  6.                 printf("error!");
  7.                 exit(1);
  8.         }
  9.         head->next=NULL;//使头结点指针域为空
  10. }
复制代码

*&head中的取地址符啥意思,为什么要加?
最佳答案
2020-12-15 10:08:20
void initialize(struct student *&head)

其中 struct student *&head 是作为型参
& 写在型参里的时候是C++语法,叫做引用,本质还是传地址。
struct student * & head 表示传递一个 struct student 指针的引用
调用的时候必须这样写
struct student  head;
struct student *p = & head;
initialize(p);
而不能直接写成
initialize(&head);

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

使用道具 举报

发表于 2020-12-14 22:02:57 | 显示全部楼层
我觉得你应该 这么看

struct student *    &head

struct student* 这是一个指针类型
&head  是一个引用  就是用head 代表一个  struct student 类型的指针
不知道我的理解是否正确,请看见的大神批评指正。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-15 08:45:36 | 显示全部楼层
  1. //成绩录入
  2. void enter(struct student *&h)
  3. {
  4.         struct student *p,*q=h;
  5.         char name[10],id[15];
  6.         int math, English,computer;
  7.         p=(struct student*)malloc(LEN);//为学生信息申请节点
  8.         printf("请依次输入学生信息:\n");
  9.         printf("姓名 学号 数学 英语 计算机导论与程序设计\n");
  10.         scanf("%s %s %d %d %d",name,id,&math,&English,&computer);
  11.        
  12.         for(;q->next!=NULL;q=q->next){;}//移动到尾结点

  13. //将输入的内容赋值给链表中的相应位置
  14.         strcpy(p->name,name);
  15.         strcpy(p->id,id);
  16.         p->score[0]=math;
  17.         p->score[1]=English;
  18.         p->score[2]=computer;
  19.         p->next=NULL;
  20.         q->next=p;
  21.         q=p;
  22. }
复制代码

还有这个,加不加取地址符到底有啥区别
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-15 09:31:50 | 显示全部楼层
我理解是这样:&head,取head的地址,*(&head)对这个地址解引用,写成**head可能更好理解。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-15 09:38:32 | 显示全部楼层
chxchxkkk 发表于 2020-12-15 09:31
我理解是这样:&head,取head的地址,*(&head)对这个地址解引用,写成**head可能更好理解。

可是在主函数中,它所对应的实参是一个struct student类型的指针,如果改成**head那么主函数就没法运行了,而且我也觉得&在这是C++中引用的意思,不是取地址
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-15 10:08:20 | 显示全部楼层    本楼为最佳答案   
void initialize(struct student *&head)

其中 struct student *&head 是作为型参
& 写在型参里的时候是C++语法,叫做引用,本质还是传地址。
struct student * & head 表示传递一个 struct student 指针的引用
调用的时候必须这样写
struct student  head;
struct student *p = & head;
initialize(p);
而不能直接写成
initialize(&head);

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-8 02:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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