Home > Backend Development > C#.Net Tutorial > What is the general form of actual parameters in C language?

What is the general form of actual parameters in C language?

下次还敢
Release: 2024-05-07 07:21:16
Original
479 people have browsed it

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.

What is the general form of actual parameters in C language?

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:

  • Passed by value: The value of the actual parameter is copied directly to the corresponding formal parameter in the function. Any modification to the formal parameters will not affect the actual parameters.
  • Pass by address: The address of the actual parameter is passed to the corresponding formal parameter in the function. Any modification in the function will directly modify the value of the actual parameter.

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;
}
Copy after login

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;
}
Copy after login

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template