gpt4 book ai didi

c++ - 按值调用和按引用调用在 C 中如何工作?

转载 作者:太空狗 更新时间:2023-10-29 23:25:14 24 4
gpt4 key购买 nike

在 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
}

关于c++ - 按值调用和按引用调用在 C 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1659302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com