What are the rules for passing formal parameters and actual parameters in the C language?

In the C language, the passing rule between formal parameters and actual parameters is by value. This means that when a function is called, the formal parameters are assigned the value of the actual parameters, not the actual parameters themselves. Therefore, any modifications made to the formal parameters inside the function will not affect the value of the actual parameters, as they are two different variables.

The rules can be summarized as follows:

  1. When a function is called, the value of the actual argument is copied into the formal parameter of the function.
  2. Parameters are local variables inside a function, modifying them will not affect the value of the arguments.
  3. The formal parameters and actual arguments of primitive data types are two independent variables, and modifying one will not affect the other.
  4. For parameters of array type, the actual address of the array is passed, so both the parameter and the argument point to the same memory area. Any modifications made to the parameter will affect the argument’s value.
  5. For pointer type parameters, the actual value being passed is the address of the pointer variable, which means the parameter and argument point to the same memory area. Any changes made to the parameter will affect the value and contents of the argument.
  6. If the formal parameter is a pointer type, it is possible to modify the value of the actual parameter by modifying the memory that the pointer points to.
  7. If the formal parameter is a pointer type and points to an array, the content of the array can be indirectly modified by changing the memory pointed to by the pointer, thereby indirectly modifying the value of the actual parameter.
bannerAds