Help determining output

Working on an exam review. We know that the output is 7, 11, 14, but we're having difficulty figuring out why. If someone could help explain/walkthrough this, that would fantastic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
using namespace std;

void mf (int &k, int *l, int m[])
{
    *(&k+1) = 11;
    *l = l[-1]+3;
    m[1] = k + 7;
}

int main ()
{
    int ma [3] = {7, 2, 5};
    mf(ma[0], &ma[2], &ma[1]);
    
    cout<<ma[0]<<", "<<ma[1]<<", "<<ma[2]<<endl;
}
You are calling mf(ma[0], &ma[2], &ma[1]), hope you have noticed the order of arguments.
ma[0] is not passed as reference, so when you print it out in the main, the initial value i.e 7 is obtained.
In the function, you write,
*(&k+1) = 11
&k will have the address of ma[0]
by writing *(&k+1) = 11, you are just storing 11 in the next address.
This is equivalent to just writing ma[1]=11.
Since you are passing a reference of the variable from main, it will be reflected when you print out the variable and you get 11.
Third statement is quite straight forward, k has 7, so m[1] is 7+7.
Topic archived. No new replies allowed.