C - 调用新函数时的指针

标签 c function pointers casting

<分区>

假设我有这样的东西:

function2(int *hello) {
    //something
}

function1(int *hello) {
    function2(&hello);
}

void main() {
    int hello = 0;
    function1(&hello);
}

如何使 function2 可以更改 main 中声明的原始值?

最佳答案

更改此代码:

function1(int *hello) {
   function2(&hello);
}

到:

function1(int *hello) {
   function2(hello);  // <-- no "&" in this call!
}

然后你可以这样做:

function2(int *hello) {
    *hello = 123;     // <-- dereference pointer hello
}

关于C - 调用新函数时的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19602300/

相关文章:

c++ - 从 c++/c 中的函数返回的问题

c - C 中最简单的 Arraylist 实现

TCP 套接字上的 send() 可以返回 >=0 和 <length 吗?

C - 在多个函数中使用结构

c++ - 使用指针表示堆栈

JavaScript 不确定代码做什么

python - 以链的形式执行列表中的函数

javascript - 如何让 io.sockets.on 调用外部/全局函数?

c - 在 C 中,从用户指定的输入字符串跳转到函数的最快方法是什么

c - 如何从 C 中的函数返回字符数组?