Joseph has a pretty solid explanation, but I'll just throw in my two cents.
p1 = &firstvalue
Whenever you access the value of p1, it's not going to give you a static value, it's going to "redirect" you to whatever value firstvalue is holding. The & operator is saying "goto the address of".
*p1 = 10
The dereference operator (denoted by an asterisk, "*") essentially means "We took the detour and are now at the location of where we were assigned to redirect to (the address of "firstvalue"). Since you are "at the location in memory", we are allowed to do whatever we want. "*p1 = " is the same thing as "firstvalue = ".
So, since we know that, then:
1 2 3 4 5 6 7 8
|
p1 = &firstvalue;
firstvalue = 10;
cout << firstvalue << endl; // Prints 10
cout << *p1 << endl; // Prints 10
firstvalue = 20;
cout << *p1 << endl; // Prints 20
*p1 = 30;
cout << firstvalue << endl; // Prints 30
|
In conclusion, you can remember both of the pointer operators as such:
& means get the location of wherever a variable is, and when assigned to a pointer, means that the pointer redirects to the location of whatever variable you assigned it to.
* means follow the path that the redirect takes you to and gets the value of wherever it leads you to. If the pointer is assigned to memory, then we go to that memory location and see what value is stored.
------
Redundant, I know, but it hammers it into your head. Pointers can be complicated but a lot of people create little analogies to help them remember (I'll be honest, writing this even helped my understanding of pointers!)