In C language, actual parameters are passed to the value of the function, while formal parameters receive the parameters of the function. There is the following relationship between them: Type matching: the formal parameter type should be compatible with the actual parameter type. Quantity matching: The number of formal parameters must be equal to the number of actual parameters. Passing by value (default): The actual parameter value is passed to the formal parameter, and modifying the formal parameter does not affect the actual parameter; Address passing (optional): Using a pointer or array actual parameter can achieve address transfer, and modifying the formal parameter can also modify the actual parameter; memory Allocation: The formal parameters are allocated memory on the stack, and the actual parameter values are copied to the formal parameter memory.
The relationship between actual parameters and formal parameters in C language
In C language, the relationship between actual parameters and formal parameters Ginseng plays different roles, but they are closely related.
Actual parameters
Formal parameters
Relationship
The relationship between actual parameters and formal parameters is as follows:
Example
For example, consider the following function:
<code class="c">void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }</code>
In this example, the actual parameters are two integers when the function is called variable. The formal parameters a
and b
are two pointers to integers. When the function is called, the values of the actual parameters are copied into the memory of the formal parameters. Operations in the function body modify the values of the formal parameters, thereby modifying the values of the actual parameters.
The above is the detailed content of What is the relationship between actual parameters and formal parameters in C language. For more information, please follow other related articles on the PHP Chinese website!