通过指针的指针来实现传出参数

来源:互联网 发布:比尔 拉塞尔 知乎 编辑:程序博客网 时间:2024/06/05 08:32

c 中所有对象包括指针都是“复制传值”,指针也不例外,可以通过指针的指针来实现传出参数,如下。

#include<stdio.h>

#include<stdlib.h>


void test(int **x) {
   int *p = malloc(sizeof(int));
   *p = 123456;
   *x = p;


}
int main() {


 int *p;
 test(&p);
 printf("%d \n",*p);


 return 0;


}
0 0
原创粉丝点击