Using pointers to pointers

This is a discussion on Using pointers to pointers within the C Programming forums, part of the General Programming Boards category; Hello, I am working on pointer to pointers. I know that a pointer is a variable that has a value ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Using pointers to pointers

    Hello,

    I am working on pointer to pointers. I know that a pointer is a variable that has a value that is an address. But I am getting confused with pointers to pointers in C.

    I have a program that I have modified to try and understand this concept.

    Is the parameter **source the address of the actual pointer?

    What is the real purpose of having pointers to pointers? I have seen them alot by looking at source code, but not completely sure why someone would want to use one. What would an alternative be in using pointer to pointers in C programming?

    Many thanks,


    Code:
    void find_integer(int **source)
    {
        // int *pint = &INTEGER;
    
    	printf("***************************\n");
    
        printf("*source = %d\n", *source);
    	printf("**source = %d\n", **source);
    	printf("Address of &*source %p\n", &*source);
    	printf("Address of &source %p\n", &source);
    	printf("Value pointed to *source %d\n", *source);
    	//printf("Value pointed to **source %d\n", **source);
    	printf("*************************\n\n");
        
    	*source = &INTEGER;
    
        printf("*source = %p\n", *source);
        printf("**source = %d\n", **source);
    }
    
    int main()
    {
        int *pint = &INTEGER;
        double *preal = NULL;
        int a = 3, b = 5;
    
    	printf("Address of Integer %p\n", &INTEGER);
    	printf("Address of pint %p\n\n", &pint);
    
        find_integer(&pint);
        return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To modify something in a function, you need to have a pointer to whatever you are modifying. If you want to modify a pointer, then you need a pointer to pointer. I have occassionally seen triple-pointers (pointer to pointer to pointer), but it's rather rare.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    53
    By the way: You write
    Code:
    printf("Address of &*source %p\n", &*source);
    but note that the & operator already means "address of". So I would write
    Code:
    printf("Address of *source %p\n", &*source);
    .

    Cheers,
    Sander

    Computer Programming: An Introduction for the Scientifically Inclined

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Another thing: when printing pointers, not only should you use the %p format specifier as you have done, but you should cast the pointer to void*, e.g.,
    Code:
    printf("Address of *source %p\n", (void*)&*source);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by Sander View Post
    By the way: You write
    Code:
    printf("Address of &*source %p\n", &*source);
    but note that the & operator already means "address of". So I would write
    Code:
    printf("Address of *source %p\n", &*source);
    .
    Oh? And btw, &* just means "dereference" then "take the address of", so &*p = p.

    And pointer to pointers are typically used for 2D arrays or when you need to pass a pointer to some function which wants to modify the original pointer.
    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.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by Elysia View Post
    Oh?
    You don't agree..?

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Technically, it would be:
    printf("Address of *source %p\n", source);
    Because as I mentioned, &*p = p.
    Last edited by Elysia; 05-28-2008 at 03:15 AM.
    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.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    If we want to use Sander's suggestion, it would be:
    Code:
    printf("Address of *source %p\n", (void*)source);
    Since p is the address of *p. On the other hand, I think it would be good for steve1_rm to also try out:
    Code:
    printf("Address of *source %p\n", (void*)&*source);
    so as to be better convinced that they are equivalent (at least if source is valid and not NULL).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Ah, I missed the "address of" part, but the &* could removed, if you wanted. It really does nothing special (compiler would probably optimize it anyway, I guess).
    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.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by Elysia View Post
    Ah, I missed the "address of" part, but the &* could removed, if you wanted. It really does nothing special (compiler would probably optimize it anyway, I guess).
    Of course. The thread starter was doing all this stuff to get a better understanding of pointers, the address-of-operator, etc., and currently takes the word of his compiler over ours... I'm sure we've all been through this stage :-)

    Also, I'm not sure the compiler is allowed to optimize it away. Is it promised that the special case that p is NULL breaks?

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Sander View Post
    Also, I'm not sure the compiler is allowed to optimize it away. Is it promised that the special case that p is NULL breaks?
    Dereferencing NULL is undefined behavior, not a mandatory failing condition. So it could optimize it.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    Dereferencing NULL is undefined behavior, not a mandatory failing condition. So it could optimize it.
    Absolutely true. In fact, code that does offsetof() uses zero-derefences to get the offset of a member:
    Code:
    #define offsetof(T, field) (size_t)(&((T*)(0)->field))
    The compiler should not actually access address zero in this case, but you have a pointer to T that is zero, which we take the address of.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    That's a specific implementation of offsetof, right?

    I think that would be undefined behavior if used in a program and not part of the implementation.
    Last edited by robwhit; 05-28-2008 at 04:29 PM.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    87
    Something I read that helped me understand pointers goes something like this:

    When you declare say an int, you say:
    Code:
    int var_name1;
    When you declare a pointer to an int, you say:
    Code:
    int *var_name2;
    The two look almost the same. If you ignore the "*" and think of it as part of the name of the variable, then you can use them the same way. So, where ever you may use "var_name1" in an expression, it would be just as valid to use "*var_name2". That is, both will give you the integer value stored in the memory referenced by the variable.

    Once you take the "*" way from the pointer's identifier, then you are talking about the location in memory, not it's contents.

    A little off topic from your original question, but hope it helps you read code better that uses pointers.
    Last edited by jason_m; 05-28-2008 at 05:03 PM. Reason: can't type

  15. #15
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    Also, I'm not sure the compiler is allowed to optimize it away. Is it promised that the special case that p is NULL breaks?
    In fact, in C99 the compiler is required to optimize it as such, meaning that applying &* to a null pointer is fine. I don't know where my C90 copy is, but the C89 draft I have doesn't require a compiler to do this.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21