Pointer Pointer #2

Pointer Pointer #1 can be found here – it deals with how the “*” symbol is used in different places when dealing with pointers.

A second pointer I have on pointers that helped me is this:

A pointer stores an address of where some piece of data “lives” (begins its bit pattern) in memory.  What recently became clear to me is that a pointer, itself,_ _is also stored as a bit pattern in memory.

Here some example code and its output to help visualize what I’m saying:

[cpp wraplines=”true”]
#include

int main(int argc, char *argv[])
{
int *intPointer;

int i = 10;

intPointer = &i;

printf(“intPointer stores the address of i, which is: %pn”, intPointer);
printf(“intPointer, itself, is stored at an address, which is different than the address intPointer stores: %pn”, &intPointer);

return 0;
}
[/cpp]

Output (on my machine):

intPointer stores the address of i, which is: 0x7fff6bacab9c
intPointer, itself, is stored at an address, which is different than the address intPointer stores: 0x7fff6bacaba0

This may or may not be obvious to you (it wasn’t to me until I understood a tiny bit of what’s happening a little further down in the computer’s layers of abstraction).  When it comes down to it, all of our C code is an abstraction.  C, and any high-level programming language removes the complexity of having to figure out how to lay down bit patterns in memory (among other things).

The key insight I gained from this discovery is that any type of variable, including pointers, that we declare and use in C code eventually translates to some bit pattern in memory that our computers use to do work with.  It took realizing that pointers, themselves were stored in memory to see this, but I’m glad it finally came to light because it’s helped de-mystify some of the mystery world of pointers.

comments powered by Disqus