Arguments vs Parameters 实参与形参
Here is an explanation of the relationship between arguments and parameters in C, and how pointers bridge the gap between them to allow for more powerful data manipulation.
1. Arguments vs. Parameters: The Basics
In C, these two terms are often used interchangeably, but there is a distinct technical difference:
-
Parameters (Formal Parameters): These are the variables listed in the function definition. They act as placeholders or local variables within that function.
-
Arguments (Actual Parameters): These are the real values or variables you pass to the function when you call it.
The Relationship: "Pass by Value"
The most important rule in C is that C always uses "Pass by Value."
When you call a function, the computer takes the value of the Argument, makes a copy of it, and stores that copy in the Parameter.
Example:
#include <stdio.h>
void changeValue(int parameter) { // 'parameter' is the Formal Parameter
parameter = 999;
printf("Inside function: %d\n", parameter);
}
int main() {
int argument = 10;
// We pass 'argument' (10) into the function
changeValue(argument); // 'argument' is the Actual Argument
printf("Inside main: %d\n", argument);
return 0;
}
Output:
Inside function: 999
Inside main: 10
Why didn't argument change in main?
Because parameter is just a copy. Changing the copy inside the function does not affect the original variable outside the function. The relationship is strictly one-way: Argument provides data to Parameter.
2. The Link to Pointers
The "Pass by Value" system creates a limitation: How do we modify the original argument?
This is where Pointers come in. To modify the original data, we don't pass the value of the data; we pass the address (location in memory) of the data.
How it works:
-
The Argument: Instead of passing the variable x, we pass &x (the address of x).
-
The Parameter: The function is defined to accept a pointer (int *p). A pointer is simply a variable designed to store an address.
-
The Relationship: The value being copied is the memory address.
Because the parameter now holds the location of the original argument, we can use the dereference operator (*) to travel to that address and modify the original data.
Example (Simulating "Pass by Reference"):
#include <stdio.h>
// Parameter is now a pointer (int *ptr)
void changeRealValue(int *ptr) {
// *ptr means: "Go to the address stored in ptr and change that value"
*ptr = 999;
}
int main() {
int argument = 10;
// We pass the ADDRESS of 'argument'
changeRealValue(&argument);
printf("Inside main: %d\n", argument);
return 0;
}
Output:
Inside main: 999
Summary of the Transformation
Here is how Pointers change the relationship between Arguments and Parameters:
| Feature | Standard Pass by Value | Pass by Pointer (Address) |
| Argument | You pass the data (e.g., 10). | You pass the address of the data (e.g., &var). |
| Parameter | Stores a copy of the data. | Stores a copy of the address. |
| Relationship | The parameter is completely independent of the argument. | The parameter points to the argument. |
| Effect | Changing parameter leaves argument untouched. | Dereferencing parameter (*ptr) modifies the original argument. |