![]() |
CITS2002 Systems Programming |
← prev | next → | ![]() |
![]() |
|||
Passing parameters by referenceRecall that when we introduced pointers in C11, we used an example attempting to swap the values of two function parameters. Because the parameters were passed by value (a copy of them was made), the original memory destinations (provided when the function was called) were not changed:
To address the problem, we employed the address-of operator so that the function would have access to the (original) memory locations. the result, however, was additional (confusing) punctuation:
To address this confusion, C++ introduces pass-by-reference parameters, which 'invisibly' treats parameters as pointers, without the additionsl syntax for dereferencing identifiers. Note that C++ still permits C11's dereferencing operator, and the code may be safely mixed within statements:
CITS2002 Systems Programming, Lecture 23, p4, 17th October 2023.
|