在C OUT参数 [英] Out parameters in C
本文介绍了在C OUT参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
void swap(int &first, int &second){
int temp = first;
first = second;
second = temp;
}
//////
//////
int a=3,b=2;
swap(a,b);
在上面的例子中,C编译器抱怨作废掉期(INT和放大器;第一,INT和放大器;二)有一个像缺少一个语法错误&放大器;之前(/ {我不明白,为什么?不支持C此功能,请澄清一下。谢谢。
In the above example, the C compiler complaints that "void swap(int &first, int &second)" has a syntax error like missing "&" before "( / {". I don't understand why? Doesn't C support this feature. Please clarify. Thanks.
推荐答案
C不支持引用传递。所以,你需要使用指针做你想达到什么样的:
C doesn't support passing by reference. So you will need to use pointers to do what you are trying to achieve:
void swap(int *first, int *second){
int temp = *first;
*first = *second;
*second = temp;
}
int a=3,b=2;
swap(&a,&b);
我的不的建议是:但我会添加它的完整性。
I do NOT recommend this: But I'll add it for completeness.
您可以使用宏,如果你的参数没有任何的副作用。
You can use a macro if your parameters have no side-effects.
#define swap(a,b){ \
int _temp = (a); \
(a) = _b; \
(b) = _temp; \
}
这篇关于在C OUT参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文