c - 如何从 Void 函数返回值?

标签 c

我有一个像这样的 void 函数,

void Charge(float p[15][11], int cantPol){
    cantPol = 0;

    for(int i = 0; i < 15; i++){        
       for(int j = 0; j < 11; j++){
           &p[i][j]
       }     
       cantPol++;
    }
    cantPol;
}

函数调用 充电(p,cantPol)

我可以像使用矩阵一样使用指针返回 trought 值“cantPol”吗?

最佳答案

Can I return trought value 'cantPol' using pointers as i did with the matrix?

一般来说是的,但是使用当前的函数声明则不能。 cantPol 是一个“按值”传递的整数。无论您在函数中做什么,当函数离开时都不会产生任何效果。

如果您想使用cantPol返回值,则必须更改参数列表以使用指针:

void Charge(float p[15][11], int *cantPol)

然后您可以为 *cantPol 分配新值,该值将在调用者处可见

您还需要调整您的函数调用。像以前一样简单地传递一个整数是行不通的。您需要传递整数变量的地址。

关于c - 如何从 Void 函数返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53533357/

相关文章:

c - 当我按 ctrl + D 时,为什么我的程序会在结束之前打印一些内容?

c - 以下程序是否会导致内存泄漏?

c++ - 循环中奇怪的指针行为

C scanf 字符数组

c - 如何以 C 程序修复我的 "Guess the Movie"游戏中的逻辑错误

c - u-boot:如何从linux用户空间访问 'bootcount'?

c - VSCode : disable code formatting on small portion of code

c++ - 如何构建这个宏

c++ - 如何在 Windows (C++ WinApi) `SUBSYSTEM:WINDOWS` 下显示和输出到控制台 (cmd)

c - c中是否有安全的 "mkdir()"类似功能?