C 中的输出参数 [英] Out parameters in C
本文介绍了C 中的输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
void swap(int &first, int &second){
int temp = first;
first = second;
second = temp;
}
int a=3,b=2;
swap(a,b);
编译器抱怨 void swap(int &first, int &second)
有语法错误.为什么?C 不支持引用吗?
The compiler complaints that void swap(int &first, int &second)
has a syntax error. Why? Doesn't C support references?
推荐答案
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 中的输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文