OK, now to talk about pointers.
Pointers are extremely important in C++, especially for handling memory. Pointers provides us a way of accessing the data in that variable without using the variable's name, in other words they can represent another way of accessing memory with an address instead of a name as with variables. In fact pointers excel excel at working with memory, not just because you can store memory addresses in them, but also because you can assign those addresses at runtime and that's the secret behind dynamic memory allocation in C++.
Every pointer you use must have a type in C++. For example a pointer that points to an integer is an integer pointer, a pointer to a double is a double pointer and so on. You can also have a void pointer which you can cast to whatever type you need at the time, which I'll talk about later on when I get the chance.
As far as C++ is concerned, memory is divided into three areas,
automatic storage where a function's variables are allocated and stored when you enter the function,
static storage, where data that is declared static is stored and left for the duration of the program's runtime and the
free store (in C it's called the "heap"). The free store is memory set aside for the use of your code at runtime, and you can allocate and handle space in the free store using the C++ new and delete operators.
Being able to allocated and free memory at runtime is a great asset to a program, it's probably important to note that when you're dealing with pointers you are also working with memory management. Being able to allocate space for many elements as you need at runtime dynamically, is in most cases far more efficient.
The
new operator returns a pointer, so when you do say a
new double, you need to assign a double pointer to it to be able to access that memory location.
Just some code to show what I mean:
CODE
#include <iostream>
using namespace std;
int main() {
double *ptr = new double;
...
In this example I've allocated space for a double variable named
ptr with the
new operator. When I'm done with the memory I would use
delete operator to free it. Another thing to note, is if new is unsuccessful, it will return a
NULL pointer which has a value of 0 and most of you would have seen error messages that tell you it's impossible to write/read from address 0x00000000, well this is usually the case when a programmer forgets to check whether new was successful or not (there are other reasons too). So always check if the pointer returned from new is not NULL else you might need to retry doing it or exit your program.
Another key thing to know about pointers is the arithmetic, the easiest thing to do is increment a pointer, and this is where a pointer is quite intelligent, a pointer will increment by the
sizeof(
type of pointer), so if I had an integer pointer and increment it by 1, it will increment by 4 bytes which is the size of an integer, and usually if you've named two integer variables and pointed the pointer to the first one, incrementing it should hopefully get you to the next named variable's address unless some form of protection is in place.
There's also similarities between pointers and arrays but that's a bit in depth for me to go into as it'd mean I'd have to explain arrays too and I'm trying to stay with pointers

I guess another thing to talk about with pointers is using them with structures. A pointer points to a data item in memory, but with a structure, you need more information. Say you wanted to point to a member of a structure and not just a whole structure itself. In that case, you need to specify exactly what member of the structure you want. So first of all you would point to the whole structure, then you could dereference it and use the
dot operator and then the member you want or you could use the proper C++ way which doesn't require the pointer to be dereferenced, using the
arrow operator. I guess another example is needed:
CODE
#include <iostream>
#include <string>
using namespace std;
int main() {
struct structure{
string text;
} structure1;
structure *ptr = &structure1;
ptr->text = "Hello, World!"; // Notice no dereference asterisk (*) and the arrow operator - preferred method
cout << (*ptr).text << endl; // Notice I'm using the dereference here and the dot operator - still works this way too
return 0;
}
As you should be able to tell, the preferred method is what you should get use to doing, however some people are use to the other method, both still work the same, so the best thing really is understanding that these two methods are exactly the same so it helps to just know these two ways otherwise you might get stumped by someone elses code (which is quite common, especially when you see their optimising techniques that makes code ugly to read just for performance).
OK, I still haven't finished, but there's really a whole lot more to talk about with pointers, things as pointers to functions, passing arguments to functions by references, pointers to objects and object members, creating a copy constructor, virtual methods and runtime polymorphism, pointers in class templates, dynamic_cast with pointers at runtime... well you should get the idea, pointer is very important and you should really get a good understanding of pointers as it will be something you'll need to use in large projects (small projects would be good too if you get use to them now)
Now most of this information is just going off by memory, I sort of stopped C++ programming a couple of years ago and just dabble in it every once in a while. I've switched over more to web environment development and tested CGI/C++ (which as far as I know, google has done a bit of that) but I'm completely happy with the web languages available and spend more time doing that than C++ these days but I'm always happy to provide what knowledge I do have of most languages I've learnt along the way, even if I rarely get the chance to visit here as much.
Cheers,
MC
Comment/Reply (w/o sign-up)