在 C 程序中,按值调用函数如何工作,按引用调用如何工作,以及如何返回值?
按值调用
void foo(int c){
c=5; //5 is assigned to a copy of c
}
像这样称呼它:
int c=4;
foo(c);
//c is still 4 here.
通过引用调用:传递指针。 C++ 中存在引用
void foo(int* c){
*c=5; //5 is assigned to c
}
像这样称呼它:
int c=0;
foo(&c);
//c is 5 here.
返回值
int foo(){
int c=4;
return c;//A copy of C is returned
}
通过参数返回
int foo(int* errcode){
*errcode = OK;
return some_calculation
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)