Is Passing Pointer Argument Pass by Value in C ?
In C , passing a pointer argument is indeed pass by value. This means that a copy of the pointer variable's contents (the address of the pointed object) is passed to the function.
Any modification made to the value of the pointer within the function body will not affect the original pointer outside the function. However, changes made to the value of the object pointed to are reflected.
Using Pointer to Pointer as Function Argument
To modify the pointer value itself within a function, a pointer to a pointer must be used as an argument. This technique is acceptable and is the standard procedure for such scenarios. By using the & operator before the pointer argument, the address of the pointer is passed.
References vs. Pointers
While using pointers to achieve this behavior is common practice, in C , using references is generally preferred over pointers for several reasons:
Drawbacks of References
In the specific case of pointer to pointers, using references simplifies the implementation. It may even be possible to eliminate the pointer levels entirely, passing a single reference instead.
The above is the detailed content of Is C Pointer Argument Passing Truly Pass-by-Value?. For more information, please follow other related articles on the PHP Chinese website!