Pointer- The deference operator
The * symbol (asterisk) can be used as
pointer symbol
dereference symbol
(and also in multiplication, e.g: 3*5 = 15)
So what is a dereference? Consider the following code sample.
In the first line, we declare a variable called num and assign it the value 3. Next we create a pointer *pNum and assign it the memory address of the variable num. The first cout statement outputs the address of num, because that is what the pointer variable pNum holds. But the last line outputs the value 3. Why? Here is where the power and the danger of pointers starts to become apparent.
Using the dereference operator, you can gain direct access to the underlying value in the variable num. If you are unsure what this means, lets add another code segment to help clarify. Copy and paste this code into a C++ app to see it working on your system if you want to test it.
What is being demonstrated here is that we are using the dereference operator to change the underlying value of num, indirectly. The two last cout lines demonstrate that the value of num and *pNum are precisely the same value (=45). You might be thinking at this point that we could simply have changed the value of num directly and you would be correct. But hold that thought for a moment and we'll revisit the importance of this later in the lesson when we discuss the purpose of pointers and their uses.
Note: & is called "amphesand"
Derefence operator (sometimes considered as direct access)
How to differentiate:
Last updated