There are two forms of actual parameter passing in C language: passing by value and passing by address. Passing by value copies the actual parameter value to the formal parameter, and modification of the formal parameter does not affect the actual parameter; passing by address transfers the actual parameter address to the formal parameter, and modification of the formal parameter directly modifies the actual parameter value. The C language defaults to pass by value, but you can use pointers to implement pass by address.
Form of actual parameters in C language
In C language, actual parameters refer to the parameters passed to the function The actual data. Actual parameters are usually passed in the following form:
In C language the default is to pass by value . However, passing by address can be implemented using pointers.
Example of passing by value:
void swap(int a, int b) { // 对形参进行交换 int temp = a; a = b; b = temp; } int main() { int x = 5; int y = 7; swap(x, y); // 按值传递 x 和 y // x 和 y 仍然是 5 和 7 printf("x = %d, y = %d\n", x, y); return 0; }
Example of passing by address:
void swap(int *a, int *b) { // 对形参(指针)进行交换 int temp = *a; *a = *b; *b = temp; } int main() { int x = 5; int y = 7; swap(&x, &y); // 按地址传递 x 和 y 的地址 // x 和 y 已被交换 printf("x = %d, y = %d\n", x, y); return 0; }
Hope this explanation can help you understand C The form of actual parameters in the language.
The above is the detailed content of What is the general form of actual parameters in C language?. For more information, please follow other related articles on the PHP Chinese website!