Today it is my goal to understand pointers.

So... any pointers?
Maybe you meant any references?:)
as I understand a pointer points to a variable.

it's usefulness is in question to me.

I hear it is to bring a variable out of scope into focus, but it does not work.

if(1) {
int myvariable = 1;
// myvariable is out of scope...
int* pmyvariable = &myvariable; //this does not work. 

I guess, what's the point?
It does not work because variable myvariable has scope only of the block scope of the if statement. So the name myvariable is uknown (undefined) outside this scope.
There are lots of uses of pointers. Here are a few:

1) When you use new to allocate an object from the heap, new returns a pointer.

2) There are lots of library calls and operating system calls that expect a pointer as an argument, or return a pointer. This is more common in C than C++.

Seriously, google "C pointers" or "C++ pointers".

This seems good: http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers

Last edited on
Manga wrote:
as I understand a pointer points to a variable.

It is essential to understand that a pointer is a variable that holds a memory address. The memory address could be the location of another variable or a function.

C++ now has smart pointers - which are a bit more advanced, but mean that we no longer need new & delete,amongst other things.

Apart from that read up about it as mutexe says.

thanks everyone... I shall consult the above mentioned link.
It is essential to understand that a pointer is a variable that holds a memory address.

This. This is probably the most important thing to understand about a pointer. It holds a number, and that number is an address in memory. When we say that a pointer "points to" some data, we mean that it contains the memory address where that data is stored.

Uses for pointers include:

- Getting around the pass-by-value problem (although in C++, we have references, which also solve this problem)

- Dynamic memory allocation

- Allowing the use of incomplete types, allowing us to remove unnecessary header file dependencies

- Array manipulation, as the name of an array is a pointer to the first element of the array (although in C++, you should be using STL container classes instead of C-style arrays, so it's best not to get into the habit of using them in the first place)

I wanted to understand pointers and I think I've got it now.

After some playing around I even made a pointer that points to the memory address of another pointer.

When I changed the value of my pointer to a pointer, it worked its way back to the original variable, and changed the value of the variable. both neat and stupid. :P

Again thanks for everyone's help. Way to go team "super awsome!"
Pointers are a tough subject for people new to C++.

I often have days where i just can't seem to make them work right and others where we're good friends. (I'm a hard core noob myself though so take what i say with a grain of salt)

What made the idea click for me was the realization that a C++ program can only do ONE THING at a time. C++ is an OOP language yes, but it still only does one thing at a time. When its inside a function doing stuff, the only thing the program knows about is what it's currently doing (the stack, the code you wrote that's executing). If you want to access something somewhere else, you need a pointer to get to it as it's in memory (the heap).

Thinking about memory as kind of like a database where you can put stuff and pull it back again seems to sit best in my head.
Another very small thing:

I find it useful to name my pointer variables with a leading p, as in:

double MyDouble = 100.00;
double *pMyDouble = &MyDouble;

Trying to use pointer variable without dereferencing them is a common source of error - mainly because someone forgot it was a pointer variable.

In C++, IMO, one should only use pointers if you need to, there are other safer alternatives.

We have references which are much better because they have to refer to a variable, unlike pointers which can be made to point to anything or nullptr - which is another source of error. One can get into strife with references, by returning a local reference from a function - which promptly goes out of scope as soon as the function ends. But on the whole they are a much safer alternative.

There is the already mentioned smart pointers.

And other things like the use of the auto keyword to help process all the elements in a container.

Hope all goes well.
Topic archived. No new replies allowed.