Changing the Value of Pointers Passed as Arguments
Get introduced to the concept of pointers to pointers.
Introduction
Our goal is to change the value of a pointer passed as an argument to a function. To be clear, we don’t want to change the variable that the pointers point to (with the dereference operator). We want to change the pointer itself and make it point somewhere else.
As a starting point, we’ll write a function called setToNULL, which receives an int pointer and sets it to NULL.
First attempt
Well, if we want to set a pointer to NULL, we usually do:
pointer = NULL;
Therefore, let’s try the same thing, but in a function. We’ll create a pointer, assign it a valid memory address, and then call a function to try to set it to NULL.
Well, this attempt is a failure. Here’s an example of the output (the address may be different for you):
[Before setToNULL]ptr holds the address: 0x7ffcc1566ee4
[After setToNULL]ptr holds the address: 0x7ffcc1566ee4
...