I'm studying C++ using a few books, trying to learn SDL along side it. I understand that pointers "point" to a variable's memory address, and that they can be used to "reference" variables. But I don't understand their purpose? And how to use them properly?
I have a few examples from the book (I've added under the code that confuses me):
#include <iostream>
int main(void)
{
char string[6] = "Hello";
char* letter = string;
letter += 3;
OK, so there's a char pointer called 'letter' that points to the memory address of string. Then somehow we use the += operator on the pointer? How? What's going add? What are we adding the 3 to?
*letter = 'p';
And now here we use '*letter' instead of 'letter' - this means it's dereferenced, right? What does this actually DO?
string[4] = '!';
std::cout << string << '\n';
system("PAUSE");
return 0;
}
The rest of the code I understand.
Thanks for any answers!
George
EDIT: so let me get this straight - dereferencing a pointer (e.g. *pointer = 2;) is used to change the value of the variable (or array position, for that matter) when you want to?
EDIT 2: thanks to everybody's answers I almost completely understand the code I used as an example - however, I am still unsure as to the use of '&' (ampersand) in the context of pointers, and how/why they're used.