C language function transfer mode-value transfer and address transfer

C language function transfer method

Pass by value

definition

  • The so-called value transfer, as the name implies, is to use variables, constants, arrays, etc. as function parameters. In fact, the value of the actual parameter is copied to the corresponding storage unit of the formal parameter, that is, the formal parameter and the actual parameter occupy different storage units.

Features

  • The characteristic of value transfer is one-way transfer, that is, when the main calling function is called, the formal parameter is assigned a storage unit, and the value of the actual parameter is passed to the formal parameter. After the call is completed, the storage unit of the formal parameter is released, and the formal parameter value Any change in will not affect the value of the actual parameter, and the storage unit of the actual parameter remains and maintains the value unchanged.

Schematic diagram

Schematic diagram of value transfer
In fact, there is an implicit step in the value transfer process, as shown in the following code:

	int x = a;
	int y = b; 

So the swap1 function exchanges x and y, and the original a and b have never changed.

Code demo

void swap1(int x,int y)
{
    
    
	int temp;
	temp = x;
	x = y;
	y = temp;
	printf("x = %d,y = %d\n",x,y);	
}
int main(int argc, char *argv[]) 
{
    
    
	int a = 12,b = 24;
    swap1(a,b); // 值传递 
    printf("a = %d b = %d",a,b);
}

operation result

Run results by value


Address pass

Description

  • Everyone knows that in the C language, the array name represents the first address of the array. The so-called address transfer refers to the function parameter is an array name or pointer. What is passed is the first address of the array or the value of the pointer, and the formal parameter receives the address, that is, the storage unit that points to the actual parameter. The formal parameter and the actual parameter occupy the same storage unit, that is, the formal parameter and the actual parameter are the same.

Features

  • There is no storage space for formal parameters, and the compiler system does not allocate memory for the parameter group. Therefore, when an array name or pointer is used as a function parameter, the transfer is only an address transfer. After obtaining the address, the formal parameter shares a memory space with the actual parameter. The change of the formal parameter is the change of the actual parameter.

Schematic diagram

Schematic diagram of address transfer
Note : There is also an implicit action here, as shown in the following code:

	x = &a;
	y = &b;

Assign the address of a and the address of b to x and y respectively, so that the numbers operated by the swap2 function are actually a and b.

Code demo

void swap2(int* x,int* y)
{
    
    
	int temp;
	temp = *x;
	*x = *y;
	*y = temp;
	printf("x = %d y = %d\n",*x,*y);
}
int main(int argc, char *argv[]) 
{
    
    
	int a = 12,b = 24;
	swap2(&a,&b); //地址传递
	printf("a = %d b = %d",a,b); 
}

operation result

Address transfer operation result
The above is the difference between C language value transfer and address transfer, I believe you should be very clear!

Guess you like

Origin blog.csdn.net/dongjinkun/article/details/104606282