Even simpler description of Pointers

The following link has an incredibly simplified explanation of pointers. Good for those of us who have never used them before.

http://alumni.cs.ucr.edu/~pdiloren/C++_Pointers/

4 Likes

Hi @rovingmind,

Thanks for sharing this very valuable resource. I’m currently reading through this and I’d like to post my progress here in case anyone else gets stuck reading through the material. In particular, I’d like to address the example given at the end of “I am pointing here! Where are you pointing?” Here an answer is given but no complete explanation of how that answer could be arrived at. Below is the original example with the answers added as well.

int a = 5, b = 10;
int *p1, *p2;
p1 = &a;
p2 = &b;
*p1 = 10;
p1 = p2;
*p1 = 20;
printf(“a = %d\n”, a);
printf(“b = %d\n”, b);
The final result is “a = 10 and b=20”

Here is my breakdown for any that are interested.

int a = 5, b = 10;

Here we are declaring two integers. The integer’s names are “a” and “b”.

int *p1, *p2

Here we are declaring two pointers that can only point to integers. The pointers names are “p1” and “p2”.

p1 = &a;

Remember “&” can be read as “address of”. Here we are instantiating p1. We are telling p1 to point to the address of a. The address is not something we can determine, but every variable has a value and an address in memory. If we were to print out the value of p1 after telling it to point to the address of a, it would print out a line of numbers representing this address.

p2 = &b;

As above, we are instantiating this other pointer to the address of b. Remember that in our declaration for p1 and p2 we determined that these pointers can only point to integers.

*p1 = 10;

Remember “*” can be read as “contents of”. Here we are setting the contents of p1 to 10. Since p1 points to a we are setting a’s value to 10. This line could be written as “a = 10;” which would have the same result.

p1 = p2

Here we are assigning p1 a new value. p1 here is told to change its value (which is an address) to the value (also an address) of p2. Now p1 will also point to b.

*p1 = 20;

This is similar to how we set the value p1 to 10 earlier, however as of the last line we changed which variable p1 points to. Since p1 now points to b we are now setting the value of b to 20.

printf(“a = %d\n”, a);
printf(“b = %d\n”, b);

Our two print lines will print out the final values of “a = 10” and “b = 20”.

I hope this breakdown helps someone else understand pointers a little better and please let me know if I have made any mistakes.

Cheers.

This website gives a good analogy to try to understand pointers. However, there seems to be an error on the “You guys are brothers?” page.

const int *tony;

This will create a const pointer called tony. Once we initialize where tony will point, we cannot point him somewhere else or his wife will be mad… I mean the compiler will give you an error.

According to C++ Primer’s (5th Ed) explanation of const and pointers, we will be able to change what ‘tony’ points to (aka. the value of tony, which is a memory address), but not the object that tony points to.

In order to match the description given on the website, it must be written like this:

int *const tony;

This will allow us to change the object that tony points to, but not the value of tony.

However, because we’re not initializing tony with an address and have not initialized it as null, this will cause bugs because we’ll never be able to change what tony points to. The computer is unable to tell if a pointer is pointing to a valid object which is why this can be an issue.

Correct however int* const tony; won’t compile, it need’s an initialiser. It also helps a ton if you read the type right to left.

Pointers to a const object, that is the object to which the pointer points to is const

const int* a; //'a 'is a pointer to int const
int const* a; //same as above

Const pointers, meaning the pointer cannot be reassigned or you might here the term “reseated”. Though would probably just use a reference instead.

int* const b; //b is a const pointer to int

Then finally const pointer to const

const int* const c; //c is a const pointer to const
int const* const c; //same

Privacy & Terms