You are familiar with an int? An int is an object that takes up an amount of memory (commonly 4 bytes, sometimes 8) and stores a single numerical value.
A pointer looks exactly the same as that. It takes up the same amount of memory as an int, and stores a single numerical value. All pointers look the same, whether they are pointers to int or pointers to double or whatever. They take up the same space and store a single numerical value.
With me so far?
Now, you know how to create an int? Like this:
int a;
You create a pointer exactly the same way;
1 2
|
int* a; // This create a pointer to an int
double* b; // This creates a pointer to a double
|
Like any object, you have to create a pointer before you can use it.
You can create a pointer to any kind of object you can think of using this syntax, but don't be fooled - the pointers all take up 4 (sometimes 8) bytes and store a single numerical value.
You know that if you create an int without assigning it a value, it'll be just some garbage value? Just whatebver happens to be in the memory at the time.
int b; // This int could have any value at all
Same thing with pointers
int* b; // This int pointer could have any value at all
Of course, the same rules for naming things apply, so this:
would refuse to compile, as I have two objects with the same name.
So far, so simple, yes?
There is one special thing a pointer can do. You recall that the pointer stores a single numerical value? I can carry out an operation called "dereferencing" on the pointer, and that simply means I get back whatever object is in the memory location of that single numerical value. So, if I my pointer stores the value 0x22334455 (I've used hex here, but it's just a number), when I dereference the pointer, I get back whatever is at memory location 0x22334455. We sometimes say that the pointer is pointing at the object in memory location 0x22334455.
How do we know what numerical value to put into a pointer (or, put another way, how do we know what the memory address of an object is, so that we can store than memory address in the pointer)? Fortunately, there exists an operator "&" which will return the address of any object (i.e. the numerical value of the place in memory where that object is).
1 2
|
int a; // an integer
&a; // the address of that integer
|
So here's how we could use that:
1 2
|
int* p; // a new pointer, with some garbage value - could be pointing anywhere
p = &a; // now we've stored the address of a in the pointer, so now the pointer is pointing at the object a
|
There are circumstances where you might need to manually enter the address location yourself, and you can do that. Say you knew for sure that a value of interest was at address location 0x2323FFFF. You can manually set it, and we'll cover that a bit later.
If all pointers are the same size and store a single numerical value, why do we have int* and double* and string* and all the rest? Because we need to know what kind of object we will get back when we dereference them. That's all. If we were being awkward, we could make an int*, and then force it to point to a double, because the pointer just stores a number - it does not store anything about what kind of thing it's pointing to. That's a bit more advanced, though.
We do this dereferencing operation with the "*". So, if I had a pointer called MrPointer, and I wanted to get what it was pointing at, I would do this:
*MrPointer;
Let's say it points at an int. I could do this:
int someInt = *MrPointer;
or I can just use it in an expression
int anotherInt = 4 + *MrPointer;
Is all this clear? If so, we can do "pointer arithmetic" which is a lot easier than it sounds.