... to function cubeByReference
using pass-by-reference with a pointer argument (line 13)—the address of number
is passed to the function. Function cubeByReference
(lines 18–20) specifies parameter nPtr
(a pointer to int
) to receive its argument. The function uses the dereferenced pointer—*nPtr
, an alias for number
in main
—to cube the value to which nPtr
points (line 19). This directly changes the value of number
in main
(line 10). Line 19 can be made clearer with redundant parentheses:
*nPtr = (*nPtr) * (*nPtr) * (*nPtr); // cube *nPtr
Get C++ How to Program, 10/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.