a question on int **

This is a discussion on a question on int ** within the C Programming forums, part of the General Programming Boards category; I have a question on the next "code", why do I pass to the function func the address of an ...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Arrow a question on int **

    I have a question on the next "code", why do I pass to the function func the address of an address? (&var)

    Code:
    int func(int **);
    
    main()
     {
        int *var;
        
        func(&var);
     }
    Thanks.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >why do I pass to the function func the address of an address?

    http://faq.cprogramming.com/cgi-bin/...1043284351#016

    [edit]
    It's not "the address of an address", it's "the address of a (pointer) variable".
    [/edit]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For a further illustration, look at this neighboring thread.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    the address of a pointer? but the address of *var is var
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    int var;
    int *varptr;
    int **varptrptr;

    var = 5;
    varptr = &var;
    varptrptr = &varptr;

    *varptr gives us 5.
    *varptrptr gives us varptr.
    **varptrptr gives us 5.

    Dereferencing a pointer gives you whatever it points to.

    Thus:

    *varptr gives you the integer
    *varptrptr gives you the pointer.

    So **varptr gives you the pointer varptr which in turn is dereferenced, giving you the 5.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    so, by & on an address you get it's value?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The & gives you the address of that variable. (IE: Where that variable is in memory.)

    Thus:
    Code:
    #include <stdio.h>
    int main( void )
    {
        int x = 5;
        printf("%p is the address of x.\n", &x );
        printf("%d is the value of x.\n", x );
    
        return 0;
    }
    To make a pointer point at something, it either has to be a direct assignment to another pointer, or the address of a variable:
    Code:
    int x;
    int *ptr1, *ptr2;
    
    x = 5;
    
    ptr1 = &x; /* make ptr1 hold the address of x */
    ptr2 = ptr1; /* make ptr2 point at what ptr1 points at */
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I understand!

    so &varptr is the address of *varptr, to match varptrptr's address, sort of speak...
    Last edited by Devil Panther; 07-04-2003 at 03:56 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can use parenthesis to illustrate exactly what happens with the double-dereference:

    int x;
    int *xp;
    int *xpp;

    x = 5; /* give a value to x */
    xp = &x; /* give a value to xp */
    xpp = &xp; /* give a value to xpp */

    The value of x is 5.
    The value of xp is the address of x.
    The value of xpp is the address of xp.

    *xp /* dereferencing a pointer gives you the value of what it points to, thus 5. */
    *xpp; /* dereferencing a pointer gives you the value of what it points to, thus the address of xp. */

    **xpp can be expanded visually to:

    *(*xpp)

    Thus, *xpp is the what xpp points to, which is a pointer. So, dereferencing that pointer gives what that pointer points to, producing 5, which is the value of x.

    One more time, "*xpp" gives you the pointer "xp". Dereferencing that pointer gives you x, and the value of 5. You can combine the two dereferences into a single step:

    **xpp


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I understand!

    so &xp is the address of *xp, to match xpp's address, sort of speak...


    Also, can i do it directly from: int x to int **x ?


    Thanks
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #11
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    But what I don't understand, is if int *xp, **xpp, why: *xpp = xp kills the program? it is support to be the same thing, no?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try this to clear things up:
    Code:
    #include <stdio.h>
    int main( void )
    {
        int x, *xp, **xpp;
    
        x = 5; /* give x a value */
        xp = &x; /* give xp a value, the address of x. */
        xpp = &xp; /* give xpp a value, the address of xp */
    
        printf("A value is what a variable holds.\n");
        printf("Pointers hold addresses of other variables as their value.\n");
        printf("To get the address of a variable, use '&'\n");
        printf("The value of 'x' is %d.\n", x );
        printf("The address of 'x' is %p.\n", &x );
        printf("The value of 'xp' is %p.\n", xp );
        printf("The address of 'xp' is %p.\n", &xp );
    
        printf("The value of 'xpp' is %p.\n", xpp );
        printf("The address of 'xpp' is %p.\n\n", &xpp );
        printf("Dereference a pointer to get what it points to.\n");
        printf("To dereference a pointer, use '*'\n");
        printf("Use the list of addresses above to see what is happening when you dereference pointers.\n\n");
        printf("Dereferencing xp gives us: %p\n", *xp );
        printf("Dereferencing xpp gives us: %p\n\n", *xpp );
    
        return 0;
    }
    Perhaps that will help clear things up.

    [edit] Fixed up %p / %d issues as illustrated below. [/edit]

    Quzah.
    Last edited by quzah; 07-04-2003 at 08:26 PM.
    Hope is the first step on the road to disappointment.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
        printf("The value of 'xp' is %p.\n", xp );
        printf("The value of 'xpp' is %p.\n", xpp );
        printf("Dereferencing xpp gives us: %p\n\n", *xpp );
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    [edit]
    Nevermind, I see what you were pointing out. That's what you ( I ) get for copy/pasting the printf lines.
    [/edit]

    Quzah.
    Last edited by quzah; 07-04-2003 at 08:23 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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