Simple Function Pointers - Determining Output

This is a discussion on Simple Function Pointers - Determining Output within the C++ Programming forums, part of the General Programming Boards category; Trace the following program to determine its output. Code: #include <iostream> using namespace std; void foo (int *a, int *b) ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    23

    Simple Function Pointers - Determining Output

    Trace the following program to determine its output.
    Code:
    #include <iostream>
    using namespace std;
    
    void foo (int *a, int *b)
    {
        *a = *a + *b;
        *b = *b + 2;
    
        cout << "a:" << *a << " b:" << *b << endl;
    }
    
    int main ()
    {
        int a = 5;
        int b = 4;
    
        foo(&a,&b);
        foo(&b,&a);
    
        cout << "a is: " << a << " b is:" << b << endl;
    }
    If somebody could go through and specify when each a,b,*a, and *b changes in the function main, or foo, that would be great.

    If I were to analyze it right now, I would say the 5 and 4 get passed into the foo function, and then *a = *a + *b; changes the a in the main function to 9, and the *b = *b + 2; changes the b in the main function to 6. Then the output from void foo would be a: 5 b: 4 (not sure about that)

    Then the 2nd time they are brought up as 6 and 9. The *a = *a + *b; changes the a in the main function from 9 to (6+9) = 15 and the *b = *b + 2; changes the b in the main function from 6 to (9+2) = 11. The output from void foo this time would be a: 6 b: 9

    Then the output from main would be a is: 15 b is: 11

    I'm not sure if that's correct so if somebody could go through it and describe what is happening at each point similarly to what I did that would be great.

    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,643
    Quote Originally Posted by Adrian View Post
    If somebody could go through and specify when each a,b,*a, and *b changes in the function main, or foo, that would be great.
    I'm not going to do that, as that would effectively be doing your homework for you. Instead, since you've had a try, I'll give pointers to where your arguments are mistaken. That way, you can think things through, and have the satisfaction of getting correct answers for yourself.
    Quote Originally Posted by Adrian View Post
    If I were to analyze it right now, I would say the 5 and 4 get passed into the foo function, and then *a = *a + *b; changes the a in the main function to 9, and the *b = *b + 2; changes the b in the main function to 6.
    Your logic is correct to here.
    Quote Originally Posted by Adrian View Post
    Then the output from void foo would be a: 5 b: 4 (not sure about that)
    Incorrect. Your function, according to your previous argument has changed it's argument *a to 9 and *b to 6, so why do you expect printing it out to yield a values of 5 and 4 (their original values) respectively?
    Quote Originally Posted by Adrian View Post
    Then the 2nd time they are brought up as 6 and 9. The *a = *a + *b; changes the a in the main function from 9 to (6+9) = 15 and the *b = *b + 2; changes the b in the main function from 6 to (9+2) = 11. The output from void foo this time would be a: 6 b: 9
    Your argument from here is incorrect. The first calls of foo() changed the values of a and b in main() to 9 and 6 respectively. The second call passes &b as the first argument of foo(), and &a as the second -- the order of arguments passed has changed.

    You are getting confused by the names of the arguments of foo() happening to be the same as the name of some variables in main(). In fact there is no relationship at all. If you change foo() so the names of the arguments are x and y, the output of the program will not change.

    It might also help if you printed a and b within main() -- particularly after the second call of foo() which passes the arguments in reverse order -- to see what is happening.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 02:11 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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