Dereferencing a pointer
We now know that a pointer may point to memory locations holding
variables' values.
It should also be obvious that if the variable's value (contents) changes,
then the pointer will keep pointing to the same variable,
(which now holds the new value).
We can use C's concept of dereferencing a pointer
to determine the value the pointer points to:
int total;
int *p = &total ;
total = 3;
printf("value of variable total is: %i\n", total );
printf("value pointed to by pointer p is: %i\n", *p );
++total; // increment the value that p points to
printf("value of variable total is: %i\n", total );
printf("value pointed to by pointer p is: %i\n", *p );
|
Even though the variable's value has changed
(from 3 to 4),
the pointer still points at the variable's location.
The pointer first pointed at an address containing 3,
and the pointer kept pointing at the (same) address
which now contains 4.
CITS2002 Systems Programming, Lecture 11, p4, 26th August 2024.
|