关于函数里对指针赋值的问题

#include <stdio.h>
#include <stdlib.h>

void test(int* a,int* b)

 b = a;
}

int main(void)
{
   int *a,*b=NULL;
   int c = 2;
   a = &c;
  //b = a;
   test(a,b);

   if(b)
      printf("aaaaaaaaaaaaa:%d\n",*b);
   else
      printf("bbbbbbbbbbbb\n");
}

为什么在函数test赋值之后b还是null?

毛病出在函数test中。编译器总是要为函数的每个参数制作临时副本,指针参数p的副本是 _p,编译器使 _p = p。如果函数体内的程序修改了_p的内容,就导致参数p的内容作相应的修改。这就是指针可以用作输出参数的原因。

正解:

#include <stdio.h>
#include <stdlib.h>

void test(int** a,int** b)

 *b = *a;
}

int main(void)
{
 int *a,*b=NULL;
 int c = 2;
 a = &c;
//b = a;
 test(&a,&b);

 if(b)
  printf("aaaaaaaaaaaaa:%d\n",*b);
 else
  printf("bbbbbbbbbbbb\n");
}

原文地址:https://www.cnblogs.com/hoys/p/2032779.html