Pointers Clarification

This is a discussion on Pointers Clarification within the C Programming forums, part of the General Programming Boards category; Code: // More on pointers and functions#include <stdio.h> void exchange (int * const pint1, int * const pint2) { int ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    40

    Pointers Clarification

    Code:
    // More on pointers and functions#include <stdio.h>
    void exchange (int * const pint1, int * const pint2)
    {
    int temp;
    temp = *pint1;
    *pint1 = *pint2;
    *pint2 = temp;
    }
    int main (void)
    {
    void exchange (int * const pint1, int * const pint2);
    int i1 = -5, i2 = 66, *p1 = &i1, *p2 = &i2;
    printf ("i1 = %i, i2 = %i\n", i1, i2);
    exchange (p1, p2);
    printf ("i1 = %i, i2 = %i\n", i1, i2);
    exchange (&i1, &i2);
    printf ("i1 = %i, i2 = %i\n", i1, i2);
    return 0;
    }

    I want to verify my understanding of the second call to the exchange function. Pasted below for convenience:
    exchange (&i1, &i2);
    printf ("i1 = %i, i2 = %i\n", i1, i2);

    So when &i1 and &i2 are pased as arguments. is the following true and what goes on in the function:
    temp = *i1
    *i1 = **i2;
    *i2 = temp;

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    So when &i1 and &i2 are pased as arguments. is the following true and what goes on in the function:
    temp = *i1
    *i1 = **i2;
    *i2 = temp;
    Not true at all. The exchange function does not change its behavior. The difference between the two calls is you set up 2 pointer variables for the first call to exchange, where the pointers point to i1 and i2 respectively, and the swap happens. In the second call to exchange you use the addresses of i1 and i2 instead. The second version probably avoids copying the arguments but that's it.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Ugh I'm confused. So this is what i think happens in the 1st call:
    temp = *p1
    *p1 = *p2;
    *p2 = temp;


    the address of &i1 is p1 and analogous terms for &i2.

    So the second call looks like:
    temp = *p2
    *p2 = *p1;
    *p1 = temp;


    How's that?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    The first time you swap, you change the value of the pointed to integer, so it's more like, *p1 equals i1 which is now equal to i2, and *p2 equals i2 which is now equal to the old i1. Then in the second swap you switch them back again: *p1 equals i1 which is now the i1 from the start, *p2 equals i2 which is now the i2 from the start.

    What makes the swap possible is the temp. Forget about variables for a second and it's always like this:
    temp = a
    a = b
    b = temp

    a is now b
    b is now a

    And your two calls to exchange, no matter what they look like, don't change this.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    The addresses of i1 and i2 never change. The values of p1 and p2 (which are the addresses of i1 and i2 respectively) also never change.

    The first call of exchange() in your code swaps the values of the variables i1 and i2. The second call swaps the values back back.

    Because of the way you have set up p1 and p2, your main() function is equivalent (if we discard p1 and p2 entirely) to.
    Code:
    int main ()
    {
        int i1 = -5, i2 = 66;
        printf ("i1 = %i, i2 = %i\n", i1, i2);
        exchange (&i1, &i2);
        printf ("i1 = %i, i2 = %i\n", i1, i2);
        exchange (&i1, &i2);
        printf ("i1 = %i, i2 = %i\n", i1, i2);
        return 0;
    }
    To put it another way, the exchange() function swaps the values pointed to by the pointers it receives, not the pointers themselves.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by pyroknife View Post
    Ugh I'm confused. So this is what i think happens in the 1st call:
    temp = *p1
    *p1 = *p2;
    *p2 = temp;


    the address of &i1 is p1 and analogous terms for &i2.

    So the second call looks like:
    temp = *p2
    *p2 = *p1;
    *p1 = temp;


    How's that?
    Yes, I see what you guys are talking about. Isn't that what I said in the above post (quote)? Or am I wrong? I thought what I wrote above is what you guys are saying.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    No.

    The thing is, you are describing it in terms of the function changing what it does on the two calls. It doesn't. If the first call is doing

    temp = *p1
    *p1 = *p2;
    *p2 = temp;


    then so is the second (since you are supplying the same arguments both times).
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    You didn't write what we said. As I told you, exchange does not change it's behavior. So, the only explanation for the changes is that you changed the underlying integers, not what the pointers point to, and definitely not using the pointers in a different order.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    I get what it's doing, but I'm having a hard time putting it in words.

    As you previously described, you are changing the values of what the pointers point to, but not what the pointers point to. So once the values of what the pointers point to are swapped, the values of the pointers p1 and p2 are also swapped.



    I switched the code to:
    printf ("i1 = %i, i2 = %i\n", i1, i2);
    exchange (p1, p2);
    printf ("i1 = %i, i2 = %i\n", i1, i2);
    exchange (p1, p2);
    printf ("i1 = %i, i2 = %i\n", i1, i2);
    return 0;

    It does the same thing, but this one is much more intuitive for me.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    So once the values of what the pointers point to are swapped, the values of the pointers p1 and p2 are also swapped.
    A pointer is a memory address, so no. You should have stopped before you said this. The rule to making a successful swap is that you need a pointer to the type of thing you want to change. If that thing is a pointer, you need a pointer to pointer, if it is an int, you need a pointer to int, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clarification....
    By CommonTater in forum C Programming
    Replies: 4
    Last Post: 09-23-2010, 05:39 PM
  2. Use Count smart pointers. Need clarification
    By Mario F. in forum C++ Programming
    Replies: 8
    Last Post: 06-26-2006, 03:07 PM
  3. Need a clarification here
    By bithub in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 12-27-2004, 12:06 AM
  4. need clarification on something...
    By EvBladeRunnervE in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-08-2004, 08:45 AM
  5. FAQ Clarification
    By Ness in forum C Programming
    Replies: 13
    Last Post: 06-10-2003, 04:37 PM

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