teach you... do you understand this?
1 2 3 4 5 6 7 8 9 10 11
|
int x[] = {1,2,3,4,5};
int index = 1;
cout << x[index];
x[index] = 20;
cout << x[index];
index = 2;
cout << x[index];
x[index] = 30;
for(int i = 0; i<5; x++)
cout << x[i];
|
index above is exactly like a pointer and the samples you are trying to understand. Index represents the pointer, and x represents the memory of your computer, but its the same idea, if you change index's value, you are looking at a different "array" location (memory location) and if you change what x at the index location (what index points to) it updates the value in the "array" (memory location).
so re looking at the question:
int secondvalue = 15; //secondvalue is a variable, so it is tied to a location somewhere in memory, and at that location in memory are some bytes that represent the value "15".
p1 = &secondvalue; //p1 is an index into memory for the location where secondvalue is stored. Or, its a pointer to the address of the variable. Same idea, different (and more correct and professionally understood) words.
*p1 = 20; // what happens here? //same as x[index] = value above, just different syntax, now its in pointer lingo instead of array terminology. you can actually USE array terminology here, p1[0] = 20; is an identical statement.
Important: they are not really the same things, of course. But this will help you understand the IDEA, I hope.