Special 3- pointer pointer variable as a function parameter

  External function address may be passed as a function pointer variable with a parameter to the internal functions, such that the internal operation of the external function can be a function of the data, and these data do not function with the end to be destroyed.

Some beginners will use the following method to exchange the values ​​of two variables:

void swap(int a, int b)
{
    int temp;  //临时变量
    temp = a;
    a = b;
    b = temp;
}

void main()
{
    int a=11, b=99;
    swap(a, b);  
}

  As a result, a, b values ​​have not changed, the exchange fails. This is because the inside of the swap function a, b, and the main function inside a, b are different variables occupy different memory. The correct approach is to use a pointer variable parameters.

void swap(int *a, int*b)
{
  int temp;
  temp = *a;
  *a=*b;
  *b=temp;        
}

void main()
{
  int a=11, b=99;
  swap(&a,&b);  
}

 

 

Guess you like

Origin www.cnblogs.com/Mike2019/p/11809239.html