Ah, this why I say that C++ gives you an understanding of the machine ;). Think of it this way:
Pointers are a way of referencing large blocks of memory without actually having to copy memory. What really happens when you call a function? Every time a function is called the compiler needs to send the parameters to the function somehow. Your computer has a stack of memory (that you usually don't manipulate directly, but the compiler knows about it). You can put data onto the stack and you can take data off of the stack. These is called push (put data on the stack) and pop (take data off of the stack) operations.
For example suppose you have a function that prints out an integer like this:
void print(int number){ somePrintFunction(number + 10); // this prints out number to the screen}void main(){ print(5);}
This is not really what happens, but for illustration purposes I'm going to explain it like this (WARNING: this is not real code either)
In the "main()" function, the parameter is pushed onto the stack and print function is called
push 5 call print
In the "print(int number)" function the parameter is popped off of the stack into a local variable called "number"
pop number push (number + 10) call somePrintFunction
Now lets make a function that prints out an array of 5 integers like so:
void print(int numbers0, int numbers1, int numbers2, int numbers3, int numbers4){ somePrintFunction(numbers0 + 10); somePrintFunction(numbers1 + 10); somePrintFunction(numbers2 + 10); somePrintFunction(numbers3 + 10); somePrintFunction(numbers4 + 10);}void main(){ int numbers[5] = { 34, 23, -2, 8, 1 } print(numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]);}
You can imagine that the "main()" function now does something like this:
push numbers[0] push numbers[1] push numbers[2] push numbers[3] push numbers[4] call print
That's obviously very slow (imagine that the array had 1000 numbers). Instead of pushing the entire array onto
the stack we'd rather push an address to the memory where numbers is stored onto the stack. This "address" is what a pointer is.
So we rewrite the function like this:
void print(int *numbers){ for(int c = 0; c < 5; c++) { somePrintFunction(numbers[c] + 10); }}void main(){ int numbers[5] = { 34, 23, -2, 8, 1 } print(numbers); // the compiler knows that when you are write numbers // without adding [] you are actually saying "a pointer to // numbers". Admittedly, this is a little confusing in the // beginning.}
And main() looks like this:
push address of numbers onto the stack call print
There's all sorts of silly things you can do with pointers. For example:
numbers[2] = 5;
is actually equivalent to
*(numbers + 2) = 5;
Sorry, I hope that's helpful. I'm not sure I wrote a very good explanation :P Other languages like python hide their pointers from you because they figure out what to do internally so that you don't have to worry about it.