Actually, everyone sucks at pointers until they learn a little bit how memory works. When a variable is created, it is given a memory address for which has been allocated the necessary amount of memory. A pointer, is a variable for which no memory was allocated and you are given the control over where it should be 'located'. This pointer you just created could point to another variable in your program (they would kind of share the same home) or it could point to a character string which is stored in a constant memory address (i.e. "hello world" will be located in some memory address for your program to access and deleted at the end of its execution). Another thing you can do with pointers is allocate memory (using the 'new' operator, don't forget to 'delete' it after) to which you will have read or read/write access.
Just like in the latter case, you don't always have write rights over a pointer. These rights are set using the 'const' keyword. Let's say you don't want the user to modify the character string to which you pointer is pointing (i.e. the first character of the string). Then it is expressed as follow:
Code:
const char* ptr = "Hello World";
This is all good, trying to modify it will cause a compiler error unless you use some trick. Now what happens if you have allocated memory for a pointer to which many other pointers are pointing ? What the hell did that mean ? Who am I ? ARgghghh ! XD Let's say you have some resource that is shared by other pointers. Let's say it's a pointer to your whole game engine's framework and let's say we have a few pointers pointing to it. If your OS decides to -- for some obscure reason -- modify the location of your pointer, you're f***ed. Why is that ? Because your other pointers are still pointing to the old memory address. Here's that situation into code:
Code:
// This is a dumb use of memory allocation
// but I couldn't think of anything else
const GameEngine* myEngine = new GameEngine;
const GameEngine* ptr = myEngine;
// Here the OS perfoms its little trick, we're f***ed
ptr->CallSomeUselessMethod(); // BANG ! Segmentation fault !
There is a way to fix this though. You need to specify that you don't want the OS to perform its nifty little trick on you by adding another 'const' before the pointer '*' symbol (it's logical, it's a const pointer), like this:
Code:
const GameEngine const* myEngine = new GameEngine;
const GameEngine* ptr = myEngine;
// The OS can't perform its little trick here
ptr->CallAnotherUselessMethod(); // You've saved yourself headaches
This is something not very common though because you rarely have many pointers to a critical object, you usually pass the object directly. I just explain further how memory works and how pointers are so intimately related to it. Now I don't want you to go around and use const pointers everywhere but just keep in mind that it exists.
[/end_of_my_teaching_trip] XD
Edit: Just noticed I'm in the C forum. The article is still valid. Replace 'new' with malloc() and 'delete' with free() (I think).