Edit: Oh dear, the following turned out quite long... Much longer than I expected...
Edit 2: Always nice to see proud, stuck-up children hijack threads...
OP:
First of all, to fully understand how pointers work - and inevitably, why they're used - you should first have an understanding of how the computer store stuff in memory.
Now, you may or may not already know this, but this topic is renowned as difficult, and maybe a little daunting, for beginners to low level programming languages, so I'll just write this down and maybe a few others reading this will learn something.
Think about the memory (RAM, harddrive, whatever) as a piece of paper with a checked pattern, like the ones you might have used in elementary school when studying mathematics. One of these squares is the smallest amount of memory you can access, and is called a bit.
A single bit can only take on the numerical value 0 or 1. So if we want bigger numbers we'll have to claim access to a chunk of bits. The following names refer to different sizes of such chunks:
byte, consists of 8 bits grouped together.
word, 16 bits grouped together - i.e. 2 bytes.
double word, 32 bits, as the name suggests, 2 words, or 4 bytes if you so will.
In C++ there are many variable types you can use, these include char, short, int, float & double.
These predefined types all have a set amount of bits allocated for them. char takes up 1 byte, short takes up 1 word (remember, 2 bytes). The size for int is system dependant, but the most common today (32bit systems, like Windows 95+) is 2 double words (i.e. 2 words, i.e. 4 bytes).
So, when you define a variable in your program, like so:
then the my_var variable is in charge of the size 1 byte takes up - i.e. 8 bits.
So, we have 8 bits we can play around with... Let's store the number 14 in our byte:
What happens here is that the number 14 is stored at our variable's address (place) in memory.
Another thing we can do is assign our variable a value of another variable:
Code:
char another = 99;
my_var = another;
This would make "my_var" contain the number 99.
So, to the left of the assignment operator (equation sign) the adress is used, and to the right the value (content) is used.
Aha!
Now, C++ lets you manually "extract" the address and use it as if it was a value.
This is done using the address-of operator like this:
Now "my_var" will contain the address of the variable named "another". This will in theory work since an address is a numerical value...
The reason I say it will only work in theory is because
A) the compiler won't let you do this assignment (more on this later), and
B) the address of a variable will not fit in 1 byte. Infact, it will be a number taking up 4 bytes (same as an int), but let's not worry about that now.
Remember, after the operation above, the variable "my_var" contains the address of the variable "another" (in theory, but bear with me). But what if we want to get the value contained by "another" through "my_var"?
It should be possible, right? I mean, we have the address of the variable we want to get the value (content) of...
Yes, C++ has another operator you can use for this. It is called the de-referencing operator and simply returns the value located at an address. Perfect.
So, if we do the following:
Code:
char anothers_value = *my_var;
then the variable "anothers_value" will infact contain the value of "another".
Now to the reason why the compiler wouldn't let you assign the address of "another" to "my_var".
The compiler would only allow this if "my_var" would have been a pointer to a char. Yep, pointers point to variables, and will thus contain the address of another variable. I.e. its value will be another variable's address...
So, how do we define a pointer? Quite simply, like this:
After this, we can make "my_pointer" point to some other variable that is of the type char.
For example, we could write this:
Code:
char my_var;
my_pointer = &my_var;
And if we later wanted to change my_var, or get its value, we could do so through the use of "my_pointer".
Actually, if we did like this:
then "my_var" would get the new value 50.
This works since we used the de-referencing operator on my_pointer, to extract the value it contains - which is the address of "my_var"!
(Remember, to the left of the assignment operator (equality sign) the address if used (in this case, the address of "my_var"), and to the right the value is used (the value of 50 here).)
But wait! I said earlier that the address of a variable takes up 4 bytes!
Yes indeed I did, and infact, every pointer takes up the same space in memory as an int (i.e. 4 bytes).
Thus, we can safely make "char pointers" for example. So, in the code above, "my_pointer" takes up the size of of an int, and "my_var" takes up the size of a char.
When should we use pointers?
As both Prelude and Mario F. implied, you will probably yourself eventually come up with situations when you want to use pointers if you just sit down and write code.
However, I could always list a few uses here, because maybe they will make more sense then...
If you have two variables, named simply "A" and "B", and say you want to change the value of either depending on some conditional, otherwise change the other, then you could do like this:
Code:
int A = 2;
int B = 3;
if (some conditional...){
A = 10;
} else{
B = 10;
}
However, pretend more things need to be done to the variable (not just settings its value to 10, but maybe also print it etc), and you decide to put that execution in a function, then you could do like this:
Code:
void change(int *P){
// first we print P using whatever method
...
// now we want to change its value to 10
*P = 10;
}
// and at some other place in our code we have the conditional:
int A = 2;
int B = 3;
if (some conditional...){
change(&A);
} else{
change(&B);
}
So, the function "change" has one argument, which is a pointer to an int, and when we call it, we give it the address of either "A" or "B".
Very much like the previous code, in which we did this:
Code:
char *my_pointer = &my_var;
remember?
I hope this "essay" helped out, in some way or another... =)