Search code examples
c++functionpointersreferencepass-by-reference

How do we properly use pointers and references to integers in a function (C++)?


I am having trouble understanding how to obtain the correct values outputted by the following C++ code when trying to solve it by hand.

#include <iostream>
using namespace std;

int f(int a, int & b,int *c){
    a += 1;
    b += 3;
    *c += 4;
    return a+b+(*c);
}
int main() {
    int a = 3, b = 4,c = 5;
    cout << "total: " << f(b,c,&a) << endl;
    cout << "a= " << a << " b= " << b << " c= " << c << endl;
    
    a = 3, b = 4,c = 5;
    cout << "total: " << f(a,a,&a) << endl;
    cout << "a= " << a << " b= " << b << " c= " << c << endl;
    return 0;
}

I know the result should be:

total: 20
a= 7 b= 4 c= 8
total: 24
a= 10 b= 4 c= 5

But everytime I try to solve this code by hand (attempt to write out the steps and assignments on a piece of paper) I can't seem obtain the correct total or values. Can anyone help me understand what is happening inside the function with a, b, and c (maybe a step-by-step explanation?). There must be something I am not understanding with the referencing/dereferencing or the pointers that is causing mistakes in my logic. Even ChatGPT is spitting out the incorrect answer after trying to execute the code...

For example if I try the first set of of inputs inside the function, here's what I understand:

  • int a = 4;
  • int &b = 5; //since c = 5
  • int *c = &a; //&a = 3

Then I start to execute the body of the function:

  • a += 1 now gives me a = 5;
  • b += 3 now gives me c = 8 since b is a reference to c so it really only is c that changes;
  • *c += 4 now takes the value stored in &a initialized as 3 and adds 4 so a's new value is 7;

The final tally is a = 7, b = 4 (hasn't changed), and c = 8. These values are correct, but the return portion of the function does not work with these values:

a+b+(*c) should be 7+4+(7), but the result of that is 18, while the correct answer is 20.

What's worst is that if I use the same logic for the second time the function is called, my values are incorrect but my total is correct... I'm lost.


Solution

  • The final tally is a = 7, b = 4 (hasn't changed), and c = 8. These values are correct, but the return portion of the function does not work with these values:

    What is getting you tripped up is that a, b, and c in your main are not the same as a, b, and c in the f(). The

    return a+b+(*c);
    

    Uses what f() knows as a, b, and c. This is part of f(). f()'s variables is the only thing that this return statement knows anything about, and it has absolutely no knowledge at all, whatsoever, about any variables in main. So to know what these variables are you just have to reread what you wrote yourself:

    a += 1 now gives me a = 5;

    b += 3 now gives me c = 8 since b is a reference

    but this b, right here is the very exact, same b that's in the return expression.

    *c += 4 now takes the value stored in &a initialized as 3 and adds 4 so a's new value is 7;

    But this is the c in the expression. So

    return a+b+(*c);
    

    computes 5+8+7 or 20, as you've observed.

    The second expression works the same way.

    You're getting tripped up by the fact that f()'s variables use the same names as the variables in main(). It also doesn't help that pointers and references from one refer to variables in the other.

    It might be helpful for you to rename the variables in f():

    int f(int x, int & y,int *z){
        x += 1;
        y += 3;
        *z += 4;
        return x+y+(*z);
    }
    

    This should be logically equivalent to the original function, but with less confusion.