链表中,关于函数参数的传递(为什么必须要用&了?)

q_v_i_t q_v_i_t 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我的链表的源程序如下,在creat_list()函数中,参数HEAD必须要加上引用,但我一直想找一下"官方解释",谢谢!!

#include <iostream.h>

struct student
{
int number;
float score;
struct student *next;
};

void creat_list(student * &HEAD) //此处的引用符号&必须要加上!
{
student *pS, *pEnd;

int number;
float score;

HEAD = new student;
pEnd = HEAD;

cin >>number >>score;

while(number != 0)
{
pS = new student;
pS->number = number;
pS->score = score;

pEnd->next = pS;
pEnd = pS;

cin >>number >>score;
}
pEnd->next = NULL;
}

void show_linkedlist(student *head)
{
cout <<"Output the items of the linkedlist:" <<endl;

student *p_show = head->next;
while(p_show != NULL)
{
cout <<p_show->number <<" " <<p_show->score <<endl;

p_show = p_show->next;
}
}
void main()
{
student * head = NULL;
creat_list(head);
show_linkedlist(head);
}
...全文
评论 12
收藏 已收藏
分享
返回顶部
q_v_i_t
C++ 语言 65,214 成员 250,514 内容