CC++ 指针笔记(一)

    科技2022-07-14  122

    指针笔记(一)

    利用传指针的方式,可以赋值给函数外面的变量。

    void functionA(int *t) { printf("%d ", *t); // should print 0 *t = 1; } int main() { int t = 0; functionA(&t); printf("%d\n", t); // should print 1 }

    在这个例子中,t是int变量。 当调用函数时,传入的是 t 的 address 给函数 functionA。而函数 functionA 的 parameter 也是指针形式的。此时 functionA 内的变量 t 和 main 函数中的变量 t 的储存位置(memory address)是一样的。 这就意味着:在函数 functionA 中更改变量 t 的值,同时也改变了 main 函数中变量t的值。 这样就可以改变函数外面变量的值了。

    输出结果:0 1

    Processed: 0.015, SQL: 8