C language: how to write a function to change the value of the pointer parameter passed in

 #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

It seems that only the function parameter is the address of the variable used in the function body can be assigned successfully.

Guess you like

Origin blog.csdn.net/aningxiaoxixi/article/details/113576606