c语言函数 & 声明

来源:百度知道 编辑:UC知道 时间:2024/05/27 11:06:35
typedef struct LNode{
int data;
struct LNode *next;
}LNode,* LinkList;

//初始化链表,表头插入N个数据
Status CreateList(LinkList &l(这里的&符号去掉了,就报错,请问参数声明时,&有什么作用),int n){
LNode * p;
l=(LNode *)malloc(sizeof(LNode));
l->next=NULL;
l->data=n;
for(int i=n;i>0;--i){
p=(LNode *)malloc(sizeof(LNode));
int e;
scanf("%d",&e);
p->data=e;
p->next=l->next;
l->next=p;
}
return OK;
}
请大家帮忙解释一下,上面的为什么要&符号

应用,意思是调用CreateList函数时第一个实参传进来,在该函数里改变了就会传回去。

举个例子:
void mTemp(int x)
{
x=1;
}

void mTempX(int &x)
{
x=1;
}
void main()
{
int s=0;
mTemp(s);
// 此时s 的结果是0
mTempX(s);
// 此时s 的结果是1
}