Hi guys, again, coming to this forum for asking some confirmation of my knowledge confusion.
firstly, let's take a look on the code:-
#include <iostream>
using std::cout;
using std::endl;
int main()
{
// main process
const int value_1 = 1000;
const int *pointer_1 = &value_1;
const int **pointer_2 = &pointer_1;
// print value; //result
cout << value_1 << endl; // 1000
cout << *pointer_1 << endl; // 1000
cout << *pointer_2 << endl; // 0x28ff1c
cout << **pointer_2 << endl; // 1000
// seperate result
cout << endl << endl << endl;
// print address; //result
cout << &value_1 << endl; // 0x28ff1c
cout << &pointer_1 << endl; // 0x28ff18
cout << &pointer_2 << endl; // 0x28ff14
return 0;
}
As you can see, pointer_2 point to 1000 through pointer_1 that point to 1000 through value_1
If you understand, you can figure out now why I'm naming the title "nested pointer", right? ;)
From the result and some paper analysis, I some sort of figure out how does the pointer works. Ignore my grammar, hope reader could understand what I'm trying to say.
I'm thought of this "timeline" of process happen in my code...
note: see my attachment for better picture of each explanation.
a. '1000' assigned to "value_1"
b. "pointer_1" become a point to "value_1" content, where:-
- *pointer_1 == value_1's content
- pointer_1 == value_1's address
c. "pointer_2" become a point to "pointer_1" content, where:-
- **pointer_2 == value's content
- *pointer_2 == pointer's content == value_1's address
- pointer_2 == pointer_1's address
then, this is what I discovered :)
1. The references operator, usually known as, "& == address of"... At first, I thought the equation is literally descriptive,
but then I discover I'm wrong. When this operator assigned to the pointer, it will return address to the "standard variable", but not only that,
it return the whole content of the address.
2. The dereference operator used as the format to the code to fill up conversion format into the language, which is the reason I put 2 "*" into pointer_2 or
the program will not compile.
3. pointer can point to the another pointer that point to a value, which in example (pointer_2 point to value_1's value through pointer_1)
there more that I discover, but can't explain or out of my knowledge, just logical expectations that useless to be discussed anyway.
btw, what you guy's opinion? =)
p.s. I post this because I'm new to this programming language, and until now(after read some book), pointer is the most complex(not much to me actually). But if confusion on basic part would cause on more complex one right? so hope you guys can confirmed this.