here is code snippet
void F (int a, int *b)
{
a = 7 ;
*b = a ;
b = &a ;
*b = 4 ;
printf("%d, %d\n", a, *b) ;
}
int main()
{
int m = 3, n = 5;
F(m, &n) ;
printf("%d, %d\n", m, n) ;
return 0;
}
answer
4 4
3 7
I see how 4 4 was computed, I don’t get how they got 3 7 (I do understand how 3 is computed, it is not changed since it was not passed by reference)
Thanks !
At the start of
main, we havethen we call
F(m, &n), passingmby value, andnby pointersuch that
Now, inside
F(), we assign7toa:then we assign
a(=7) to the memory address pointed byb(->n)next we change
b, so that nowbpoints toa:and then we assign 4 to the memory address pointed by
b(->a)printing
a(=4) and*b(->a =4)and printing
m(=3) andn(=7) outside the function