Pointers 45
Shown below is the call stack after the program finishes the fourth line.
Symbol Address Value
iptr 102 100
c 101 garbage
a 100 632
The fifth line does the following things:
1. Takes iptr’s value as an address. The value is 100.
2. Goes to that address (100).
3. Reads the value at that address and it is 632.
4. Assigns 632 to c.
After the fifth line, the call stack becomes:
Symbol Address Value
iptr 102 100
c 101 632
a 100 632
This is the rule: If a program has something like the following:
= * iptr ; // iptr is a pointer1
the program will
1. take iptr’s value as an address.
2. go to that address.
3. read the value at that address.
4. assign the value to the variable at the left.
This rule is applicable if * iptr is at the right hand side (RHS) of the assignment sign (=).
This is also called dereferencing a pointer. The assignment sign = is not strictly necessary,
in which case the rule still works without part 4 of the assignment. For example, the eighth
line of the code above prints 632.
The emphasis on the “right hand side” is important. When * iptr is on the left hand
side (LHS), it works in a similar but opposite way. For example, the last line of code above
does the following:
1. Takes iptr’s value as an address and it is 100.
2. Goes to that address (100).
3. Modifies the value at address 100 to −84.
Thus, after the last line, the call stack becomes
Symbol Address Value
iptr 102 100
c 101 632
a 100 −84
Many students find pointers confusing at first. This confusion is well justified. The same
symbol * has different meanings. The symbol also means multiplication when it is between
two numeric values (integer, float, double). The following table summarizes the different
meanings:
It is time to test your understanding of the different usages of *. Draw the call stack for
the following code snippet: