Thread: Reference to a pointer to a pointer

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Reference to a pointer to a pointer

    Exercise 7 from Chapter 11 of Bruce Eckel's "Thinking in C++":

    Create a function that takes an argument of a reference to a pointer to a pointer and modifies that argument. In main( ), call the function.

    I'm having difficulty figuring out exactly what I can pass as an argument to a function which takes an argument of that kind. For example:

    Code:
    void f1(int**& x)
    {
    	x++;
    }
    
    
    int main() 
    {
    	int x = 0;
    	int *px = &x;
    	int **ppx = &px;
    	f1(ppx); // fine
    	//f1(&px) doesn't work
    
    	return 0;
    }
    I guess what I'm having trouble understanding is why I can pass ppx but not &px.

  2. #2
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Gah! It didn't take long after I'd hit "submit" that I realized exactly why I can't pass it.

    Move along folks, nothing to see here.....

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Experiment:
    What if you do:
    Code:
    void f1(const int** & x)
    {
    	//x++;
    }
    
    
    int main() 
    {
    	int x = 0;
    	int * px = &x;
    	f1(&px);
    
    	return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    I'll get exactly the same error as I did as before when I tried to compile that same function call, i.e:

    Code:
    error C2664: 'f1' : cannot convert parameter 1 from 'int **' to 'const int **&'

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    37
    Quote Originally Posted by Sharke View Post
    Gah! It didn't take long after I'd hit "submit" that I realized exactly why I can't pass it.

    Move along folks, nothing to see here.....
    This is a prime example of the 3rd rule of programming:

    "Right after you ask for help and have shown your code to the world, you will realize your stupid mistake and be humiliated"

    I live by this rule :-)

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Sharke View Post
    I'll get exactly the same error as I did as before when I tried to compile that same function call, i.e:

    Code:
    error C2664: 'f1' : cannot convert parameter 1 from 'int **' to 'const int **&'
    Ah yes, the evil placement of const.
    This one should work:
    Code:
    void f1(int** const & x)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Pointer to a pointer to a reference to const?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    More like a reference to a constant pointer to int**.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    *head swims*

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm sure it's a little difficult to get your head wrapped around, but there are two different types of pointers, basically.
    const T* -> A pointer to type const T.
    T* const -> A constant pointer to type T.

    When I placed the const all to the left in the beginning, it became part of the type the pointer pointed to!
    But what we actually need a constant pointer.
    We can pass temporaries by const reference, and const reference means a reference to a constant type (not a pointer TO a constant type--that's something entirely different!).

    Meh. This may be a little difficult to wrap your head around, but you aren't required to learn it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by Elysia View Post
    This may be a little difficult to wrap your head around, but you aren't required to learn it.
    Maybe not, but the less dangling threads I leave when I'm learning something, the better! I had just about wrapped my head around const placement in C, I guess it was the addition of references that tipped me over the edge.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Everything to the left of the & of a reference is the type the reference points to.
    Similarly for pointers, everything left of the * is the type it points to.
    But remember that a pointer is a variable itself, so it too, can be const. You just have to place it to the right of the *.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Got it. But perhaps I'm not understanding the whole passing temporaries thing. Suppose the following:

    Code:
    class X
    {
    public:
        X() {}
    };
    
    X f1()
    {
        X x;
        return x;
    }
    
    void f2(X& x) {}
    
    int main() 
    {
        f2(f1());
    }
    I'm confused that the compiler sees nothing wrong with me passing the result of f1() into f2() even though f2() doesn't ask for a const reference.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Sharke View Post
    Got it. But perhaps I'm not understanding the whole passing temporaries thing. Suppose the following:

    Code:
    class X
    {
    public:
        X() {}
    };
    
    X f1()
    {
        X x;
        return x;
    }
    
    void f2(X& x) {}
    
    int main() 
    {
        f2(f1());
    }
    I'm confused that the compiler sees nothing wrong with me passing the result of f1() into f2() even though f2() doesn't ask for a const reference.
    I'm surprised you call
    Code:
    temp.cpp:17: error: invalid initialization of non-const reference of type ‘X&’ from a temporary of type ‘X’
    temp.cpp:13: error: in passing argument 1 of ‘void f2(X&)’
    "nothing wrong".

  15. #15
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    This is what Visual C++ Express gives me with the exact same code:

    Code:
    0 error(s), 0 warning(s)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    EDIT: The compiler's warning level was set to level 3, the default. This is what I get after jacking it up to level 4:

    Code:
    warning C4239: nonstandard extension used : 'argument' : conversion from 'X' to 'X &'
    1>        A non-const reference may only be bound to an lvalue
    So, not an error, only a warning - and only after the warning level is raised higher than the default.
    Last edited by Sharke; 05-25-2009 at 03:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  4. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  5. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:08 AM