Thread: Question about Pointers

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    29

    Question Question about Pointers

    Is this function
    Code:
    void sum_and_product(int *pa, *int pb)
    {
      int sum = *pa + *pb;
      int diff = *pa - *pb;
     
      *pa = sum;
      *pb = diff;
    }
    equivalent to this one? -
    Code:
    void sum_and_product(int *pa, *int pb)
    {
      int sum = *pa + *pb;
      int diff = *pa - *pb;
     
      pa = ∑
      pb = &diff;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No, the second changes the addresses the pointers point to, which luckily for you won't be updated in the calling code (since they are addresses to local variable which go out of scope).

    Otherwise, it looks somewhat strange that you'd want to completely lose the original data.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    87
    Quote Originally Posted by anon View Post
    ...which luckily for you won't be updated in the calling code...
    data.
    What do you mean by this? I figured the behavior would be to change the addresses, the function returns, the memory locations held by sum and diff are discarded (they are popped off the stack) and values of the variables passed as arguments to the function are now unreliable.

    Jason

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    pa and pb are values passed to the function. As such, any changes made to those values are discarded at the end of the function.

    In other words, you can change what a pointer points to in a function, but not where a function points, if you want the changes seen in the calling code.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    87
    Right. I need a 1 hour wait rule after getting out of bed before being able to post :-p. It occurred to me how wrong my post was when I got in the shower and I almost bolted back out to the computer to edit it before anyone could see it, but oh well.

    So yes, the parameters pa and pb are two newly created pointers to ints setup when sum_and_product(...) is called. They are not the original arguments supplied to sum_and_product, but rather hold the addresses of the variables supplied as those arguments.

    Changing the addresses held by pa and pb is fine, now they point to sum and diff. So, if you were to change sum later in the function, *pa would produce this new value. However, you've lost any means by which to update the arguments supplied in the calling function.

    Jason

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To change ANYTHING in a function, you need it's address (or in other words, it needs to be a pointer to what you want to change).

    E.g
    Code:
    void func(int a)
    {
       a = 7;
    }
    
    int main()
    {
       int x = 18;
    
       func(x);  
       ...
    x is 18, and the number 18 is passed to func in a, a is set to 7, but x is still 18 when you get back to main.

    If we change it to be a pointer:
    Code:
    void func(int *a)
    {
       *a = 7;
    }
    
    int main()
    {
       int x = 18;
    
       func(&x);  
       ...
    Now x would be 7 when we get back from func to main.

    If you want to change what a pointer points to, you would have to pass a pointer to a pointer.

    And finally, if you ever take an address of a local variable, and use that address after you have returned from the function that had the local variable, don't expect it to work - that variable is no longer the variable you took the address of - it is either free space on the stack or being used for something else.

    --
    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.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    29

    Thanks!

    That really cleared things up!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

Tags for this Thread