c语言:如何编写函数,改变传入的指针参数的值
原创
©著作权归作者所有:来自51CTO博客作者wx62df820aeac21的原创作品,请联系作者获取转载授权,否则将追究法律责任
#include<stdio.h>
int source =1;
int* cope_adress(int *a)
{
a=&source;
}
int main()
{
int b=2;
int *purposes;
printf("source address is %p\n",&source);
purposes=&b;
cope_adress(purposes);
printf("cope_adress is %p\n",purposes);
}
*************************************
*************************************
book@xxx:~/work$ g++ myname.c
book@xxx:~/work$ ./a.out
source address is 0x601040
cope_adress is 0x7ffdad9a7b7c
#include<stdio.h>
int source =1;
int* cope_adress(int **a)
{
*a=&source;
}
int main()
{
int b=2;
int **purposes;
printf("source address is %p\n",&source);
int *myb=&b;
purposes=&myb;
cope_adress(purposes);
printf("cope_adress is %p\n",*purposes);
}
*************************************
*************************************
book@xxx:~/work$ gcc 1.c
book@xxx:~/work$ ./a.out
source address is 0x601040
cope_adress is 0x601040
看上去,只有 函数形参 是函数体内的使用的变量的地址才可以赋值成功。