链表中,关于函数参数的传递(为什么必须要用&了?)
我的链表的源程序如下,在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);
}
#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);
}
...全文
相关推荐
C语言 指针变量作为函数参数详解
本文主要介绍C语言 指针变量作为函数参数,这里整理了相关资料和示例代码,以便大家学习参考理解知识点,有需要的小伙伴可以参考下
单链表操作中指针作为函数参数的典型错误.cpp
单链表操作中指针作为函数参数的典型错误.cpp
Python中 传递值 和 传递引用 的区别解析
主要介绍了Python中 传递值 与 传递引用 的区别解析,非常不错,具有参考借鉴价值,需要的朋友参考下吧
单链表的就地反转
单链表的就地反转
C语言创建链表错误之通过指针参数申请动态内存实例分析
主要介绍了C语言创建链表错误之通过指针参数申请动态内存,是链表创建过程中非常常见的经典错误。实例中做了较为详尽的分析,需要的朋友可以参考下


