Question:
In C , when passing pointer arguments, does it behave as pass by value or pass by reference?
Answer:
Pointers are passed by value in C , meaning the address of the pointed object is copied into the function parameter. This implies two scenarios:
Pointer to Pointer Passing:
If the goal is to change the pointer value itself (point it to a different object), a pointer to pointer is required. This is achieved by using a double ampersand (&&) before the pointer name when passing it to the function. This method is standard practice in C.
References vs. Pointers:
In C , references are preferred over pointers for several reasons:
Advantages of References:
Drawbacks of References:
Specific Case: Pointer to Pointer vs. Reference
In the case of pointer to pointer passing, the main difference with using a reference is simplicity. By passing a reference to the outer pointer, both levels of indirection can be eliminated, providing a more straightforward approach.
The above is the detailed content of How are Pointers Passed in C : By Value or By Reference?. For more information, please follow other related articles on the PHP Chinese website!