Home > Backend Development > C++ > Is C Pointer Argument Passing Truly Pass-by-Value?

Is C Pointer Argument Passing Truly Pass-by-Value?

DDD
Release: 2024-12-15 02:37:09
Original
183 people have browsed it

Is C   Pointer Argument Passing Truly Pass-by-Value?

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:

  • Reduced syntactic complexity in function bodies
  • Provision of more information to the compiler

Drawbacks of References

  • Break the pass-by-value rule, requiring attention to parameter behavior
  • Not supported by C

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!

source:php.cn
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