Confused with pointers and pass by reference

This is a discussion on Confused with pointers and pass by reference within the C Programming forums, part of the General Programming Boards category; Originally Posted by AndiPersti [code] Basically *&i == i, because & and * cancel. That was my point at the ...

  1. #16
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by AndiPersti View Post
    [code]
    Basically *&i == i, because & and * cancel.
    That was my point at the above post!

    About tou example i think i got it.But let's check to be sure
    Code:
    #include <stdio.h>
     
    int main(void)
    {
        int i, *ptr1, **ptr2, ***ptr3;
     
        i = 10;
        ptr1 = &i;
        ptr2 = &ptr1;
        ptr3 = &ptr2;
     
        printf("%d\n", i == **ptr2);
        printf("%d\n", i == **&ptr1);
        printf("%d\n", *&i == ***ptr3);
        printf("%d\n", &i == **&*&ptr2);
     
        return 0;
    }
    ptr2 is a 'double' pointer so because of line 9 points to ptr1 and then points(finally) to i.So line 12 will output 1.
    Line 13 * cancels & so we have i==*ptr1,of course true,so output 1.
    Line 14 is equivalent to i==***ptr3 which is true,becaues ptr3 is a 'triple pointer' which first points to ptr2(line 9),then points to ptr1 and finally points to i(the value of i).So output 1.
    Line 15 is equivalent to &i==*ptr2 .ptr2 is a double pointer,so it points first to ptr1 and finally at the value 1.So *ptr2 is going to give us the value of ptr1,which is the adress of i and so output is going to be again 1.
    Correct?

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by std10093 View Post
    By writing
    Code:
    new_mysql_connection(&conn);
    it is like 'killing' the pointer
    Remember that & 'kills' the *
    For me "killing" means to get rid of something (it's destructive).
    When you add the & operator to a function argument, you don't get rid of the pointer but you construct a pointer to that argument (it's constructive).

    That's why I think "killing" is misleading.

    Bye, Andreas

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

Similar Threads

  1. Replies: 4
    Last Post: 02-14-2012, 07:45 PM
  2. Pass by value/reference
    By Niels_M in forum C Programming
    Replies: 14
    Last Post: 11-07-2010, 01:35 PM
  3. Confused as to how to pass back to main
    By J-Camz in forum C Programming
    Replies: 6
    Last Post: 11-28-2008, 07:21 AM
  4. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  5. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 02:03 PM

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