参数传递-引用传递中c和cpp的区别

来源:百度知道 编辑:UC知道 时间:2024/05/30 21:07:29
引用传递
void Exchang3(int &x,int &y)
{
int tmp;
tmp=x;
x=y;
y=tmp;
printf("x=%d,y=%d\n",x,y);
}

void main()
{
int a=1,b=2;
Exchang3(a,b);
printf("a=%d,b=%d\n",a,b);
}
编译报错
--------------------Configuration: p4 - Win32 Debug--------------------
Compiling...
p4.c
E:\p4.c(21) : error C2143: syntax error : missing ')' before '&'
E:\p4.c(21) : error C2143: syntax error : missing ')' before '&'
E:\p4.c(21) : error C2143: syntax error : missing '{' before '&'
E:\p4.c(21) : error C2059: syntax error : '&'
E:\p4.c(21) : error C2059: syntax error : ')'
执行 cl.exe 时出错.

p4.exe - 1 error(s), 0 warning(s)

将后缀改为cpp编译通过并得出

为什么改为cpp才可以呢?C++在参数传递中多了些什么?
将后缀改为cpp编译通过并得出

c语言不支持引用传参

c++才支持

后缀名为c会当做c语言来编译

cpp当做c++来编译

void Exchang3(int &x,int &y)

C不支持 int &x。
这个在cpp中貌似叫做“引用”?我搞C的,cpp只是简单看过,貌似是引用。。。

楼下上吧

你改成C++文件(cpp)后,调用的是C++的库来编译的,所以就可以了.不过在C中可以用指针.