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.

Recommended Answers

All 9 Replies

If you understand, you can figure out now why I'm naming the title "nested pointer", right?

I was pretty confident that I knew why just from reading the title.

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

If I were your teacher, I'd give you perfect marks for this.

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.

Now you're getting into nonsensical territory. The address-of operator is literal. It evaluates to the address of an object, and a pointer object isn't treated differently. The problem with pointers and addresses is people get confused because a pointer is both an object with an address and an object that stores an address. So for an initialized pointer there are two addresses and two values involved:

  • &ptr : The ptr object's address.
  • ptr : The ptr object's value (the pointed-to object's address).
  • *ptr : The pointed-to object's value.

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.

I can't even begin to respond to this, it's completely incoherent.

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)

Yes. And this behavior is recursive for all levels of indirection. A pointer can hold the address of another pointer, so you can continue to tack on levels of indirection. For example:

int i = 123;
int *p = &i;
int **p2 = &p;
int ***p3 = &p2;
int ****p4 = &p3;
int *****p5 = &p4;
...

Though you won't see more than two levels often, and more than three are virtually non-existent.

But if confusion on basic part would cause on more complex one right?

Absolutely. Pointers are actually very simple. A combination of fear mongering among teachers (eg. "pointers are hard, so listen up") and over-complication among both students and teachers is what causes them to be complex.

The reality is thus: A pointer is an object that stores the address of (ie. "points to") another object. The address of an object can be found with the address-of operator ( &obj ), and the value of a pointed-to object can be found with the indirection operator ( *ptr ). Indirection can be applied as long as the object is a pointer, so "recursive" pointers are allowed.

commented: great +14

I was pretty confident that I knew why just from reading the title.

ah, the google's result just not helping, at the point where I thought "maybe there are specified definition for these, well, time to go for my self-defined word again" :P

If I were your teacher, I'd give you perfect marks for this.

thnx :)
programmers don't mind typo, eh? (except in code, :P)

Now you're getting into nonsensical territory. The address-of operator is literal. It evaluates to the address of an object, and a pointer object isn't treated differently. The problem with pointers and addresses is people get confused because a pointer is both an object with an address and an object that stores an address. So for an initialized pointer there are two addresses and two values involved:

&ptr : The ptr object's address.
ptr : The ptr object's value (the pointed-to object's address).
*ptr : The pointed-to object's value.

well, what I mean about 'taking it literally' is when I bump into this code :-

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int firstvalue, secondvalue, thirdvalue;
    int *mypointer;

    mypointer = &firstvalue;
    *mypointer = 10;
    mypointer = &secondvalue;
    *mypointer = 20;

    cout << "firstvalue is " << firstvalue << endl;
    cout << "secondvalue is " << secondvalue << endl;

    cout << endl << endl << endl;

    cout << "mypointer  is " << mypointer << endl;
    cout << "first address is " << &firstvalue << endl;
    cout << "second address is " << &secondvalue << endl;

    return 0;
}

I thought & == just a reference to the address and nothing more... which brought me some confusion when I analyse the main post's code. That's why I declare such statement, but I take yours as better explained!

Well, I told already that I have problem with english, especially when trying to say something... because based on my experience asking question on this forum or any other site, my question/statement always seem vague and hard to completely understood

I can't even begin to respond to this, it's completely incoherent.

hmm, that's must mean to be "I need to keep this to myself until I get more into computer science base"

btw, anyone know which book good to give understanding of computer science?

A combination of fear mongering among teachers (eg. "pointers are hard, so listen up") and over-complication among both students and teachers is what causes them to be complex.

ha3, that's give me some sort of weird relief as a self-taught student (C++ Primer, Fourth Edition, btw) ;)

A pointer is an object that stores the address of (ie. "points to") another object. The address of an object can be found with the address-of operator ( &obj ), and the value of a pointed-to object can be found with the indirection operator ( *ptr ). Indirection can be applied as long as the object is a pointer, so "recursive" pointers are allowed.

thnx. I will keep this in mind =)

btw, about other logical discoveries that I had in my mind, hmm, it's just a bunch of mathematical related reasoning, I'm not going to bother much, thought so it's developing overtime through this programming book. That's why I hope there any computer science's book that has direct explanation to what I'm thinking about.

A nested pointer **i is the same thing as a two dimesional array int i[][] .

Thus

int **p2 = p2[][]
int ***p3 = p3[][][]

etc etc

A nested pointer **i is the same thing as a two dimesional array int i[][] .

No, that's wrong. Though pointers can be used to simulate arrays of varying dimensions, they're not the same thing.

Narue,

Thank you very much for your outstanding explanation of pointers. Like the OP, I am also self-learning C++. Your post has raised my understanding of pointers several levels, as the books and tutorials I read were nowhere near as clear.

I have a follow-up question. (Not to hi-jack the OP's thread, but it is very relevant.) Does nesting/recursion work for the dereferencing operator as well?

int i = 123;
int *p1 = &i;
int **p2 = &p1;
int ***p3 = &p2;
...
int &i = *p1;
int &&i = **p2;
int &&&i = ***p3;

I am suspecting not, as I can't reason through a logical pathway.

Does nesting/recursion work for the dereferencing operator as well?

Certainly:

#include <iostream>

int main()
{
    int i = 123;
    int *p1 = &i;
    int **p2 = &p1;
    int ***p3 = &p2;
    
    std::cout << &p2 << " -- " << p3 << '\n';
    std::cout << &p1 << " -- " << *p3 << '\n';
    std::cout << &i << " -- " << **p3 << '\n';
    std::cout << i << " -- " << ***p3 << '\n';
}
int &i = *p1;
int &&i = **p2;
int &&&i = ***p3;

Applying the & operator to a type is something completely different. It only works as the address-of operator when applied to an object. Specifically, the first line defines a reference to an int, the second line defines an rvalue reference to an lvalue (not legal), and the third line is a syntax error.

@Nathaniel10

thnx for your input, from Narue's explaination, I think I figure out something from these reference and dereference operator..

as a lvalue:-
    - int  variable; // where: this is 'standard variable'
    
    - int* variable; // where: this one hold the address and content of the assigned
                     // variable. yet, the content and address that it keep will 
                     // become a content of another memory slot with it own address.
    
    - int& variable; // where: this one is a 'reference type'(any act on this variable
                     // make the variable it referred have the same affect as
                     // this one)


as a rvalue:-
    -  variable; // return the 'standard value'.
    - *variable; // return the value it referred.
    - &variable; // return the address of the value it referred.

well, correct me if I'm wrong.. =)

OK, Narue. Thanks for your reply.

I think I understand the first part of it.

For line 13, the two values should be equal to each other and be 123.
For line 12, the two values should be equal to each other and be the address of variable i.
For line 11, the two values should be equal to each other and be the address of pointer p1.
For line 10, the two values should be equal to each other and be the address of pointer p2.

For the second part, I referred back to my Stroustrup book. It does discusses how the * and & operators do not function identically(?) for chars and ints. It advises the use of reinterpret_cast to deal with conversions of type. I am not at all clear on this, but fortunately, do not need that knowledge in the near future. I will read more on how types affect pointer and de-reference operators.

It does discusses how the * and & operators do not function identically(?) for chars and ints.

Once again, this is different from pointers in general. It's a specific matter of using pointers to view an object as a different type:

int i = 123;
char *p = (char*)&i; // View i through char colored glasses

This is called type punning, and while pointers make it possible, it doesn't change how pointers work. The * and & operators still do the same thing in the same way. Note that I used a cast to force the result of &i (which is int*) into char*. That's what Stroustrup is talking about with the reinterpret_cast being a better choice. It doesn't change the effect, it just makes your intentions more clear:

int i = 123;
char *p = reinterpret_cast<char*>(&i); // View i through char colored glasses

I will read more on how types affect pointer and de-reference operators.

Read more on pointers vs. references, that's what the difference is. It's a shame that the same operators are reused for completely different purposes, because that's likely to confuse you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.