in Programming in C edited by
17,986 views
58 votes
58 votes

Consider the following C program.

# include <stdio.h>
void mystery (int *ptra, int *ptrb) {  
     int *temp; 
     temp = ptrb;  
     ptrb =ptra; 
     ptra = temp; 
} 
int main () { 
    int a = 2016, b=0, c= 4, d = 42; 
    mystery (&a, &b);
    if (a < c) 
          mystery (&c, &a); 
    mystery (&a, &d); 
    printf("%d\n", a); 
}

The output of the program is _________.

in Programming in C edited by
18.0k views

4 Comments

 mystery about mystery function :D

2
2
just see there is no * in mystery so basically they just swapped pointer with each other doesn’t change anything then
2
2
beautiful question
0
0

3 Answers

84 votes
84 votes
Best answer
The mystery about mystery function is it does not affect values in main. As in $C$, parameters are passed by value- even if they are pointer. So, here the pointer values are exchanged within the function only. (we can use $*$ operator to exchange the values at the location of the pointers and this will affect the values in main).

So, NO CHANGES in $a,b,c,d$.
And ANSWER is $2016$
edited by

14 Comments

this was the most tricky ques in set 1 i feel :(
1
1
it is passed by reference ? Why doesn't it change ?
0
0
There is no pass by reference in C language.
25
25
if we use * then o/p will be 42
1
1

The mystery about mystery function is there is no mystery, this function does nothing, and i don't know why people calling it interesting. 

This function is same as swap function in this Gate question gate2006-it-50.

27
27

ya i m agree sachin mittal .  here people are thinking like that  *temp = *ptrb; *ptrb=*ptra; *ptra=*temp;      but that is wrong bcz here temp is a pointer variable means which will holds the adrees and here *ptrb gives some value but not address .and moreover they given temp=ptrb// temp holds the value of ptrb and ptrb value itself an adress so temp holds same adress as ptrb . and same thing for all and finally we get a value remain same which is 2016

2
2
Can someone please explain me what to do to swap the value?
0
0
void mystery (int *ptra, int *ptrb) {  
     int temp; 
     temp = *ptrb;  
     *ptrb =*ptra; 
     *ptra = temp; 
} 

this will swap original value

4
4

@Arjun Sir

There is no call by reference in C language. 

Then can u tell me, why in this question in function f2(), value changing by pointer, I mean, why b and c value swapped ??

0
0
You need to understand what is "call by reference". It is when a programmer passes a variable and its address gets copied to the actual parameter (the actual parameter must use the address of the passed parameter). This never happens in C. But this effect can be simulated by passing a pointer and dereferencing it -- which is exactly what is happening above. In C language we cannot pass a function to another function -- but this can be simulated by passing a pointer to the function.
8
8
It would have been 4 if was pass by reference as 2016 would be swapped with 0 and 0 would be swapped with 4 and returned 4.
0
0
Why *ptrb give value here not address.
0
0

@Arjun Sir, can you give an example where actual “call by reference” is happening rather than passing a pointer and dereferencing it.

0
0
After seeing the function MYSTERY I remembered a few lines by Master Oogway and would like to share with you guys…

“Yesterday is Mystery...

Tomorrow is History…

But Today is a GIFT…

And that’s why it’s called Present” :)
1
1
29 votes
29 votes

We can observe this using the memory stack. First main()  is pushed onto the stack, then mystery(&a,&b) is pushed onto the stack.
Now in the mystery function the addresses gets swapped. After the end of the function ptra points at 0 and ptrb at 2016. BUT these addresses are lost as when mystery is popped out and control goes back to main.
ptra and ptrb are local to the function only. had it been the case that we change the values at the memory locations then the change would have been permanent.
 

#include <stdio.h>
void mystery (int *ptra, int *ptrb) {  
     int *temp; 
     printf("1.Values are *ptra : %d\t*ptrb:%d\n",*ptra,*ptrb);
     printf("Values are ptra : %d\tptrb:%d\n",ptra,ptrb);
     temp = ptrb;  
     ptrb =ptra; 
     ptra = temp; 
     printf("2.Values are *ptra : %d\t*ptrb:%d\n",*ptra,*ptrb);
     printf("Values are ptra : %d\tptrb:%d\n",ptra,ptrb);
} 
int main () { 
    int a = 2016, b=0, c= 4, d = 42; 
    printf("Address of a is %d \tValue of a is %d\n",&a,a);
    printf("Address of b is %d \tValue of b is %d\n",&b,b);
    mystery (&a, &b);
    printf("Address of a is %d \tValue of a is %d\n",&a,a);
    printf("Address of b is %d \tValue of b is %d\n",&b,b);
    if (a < c) 
          mystery (&c, &a); 
    mystery (&a, &d); 
    printf("Value of a is %d\n", a); 
}

Output is

Address of a is 1724577616     Value of a is 2016
Address of b is 1724577620     Value of b is 0
1.Values are *ptra : 2016    *ptrb:0
Values are ptra : 1724577616    ptrb:1724577620
2.Values are *ptra : 0    *ptrb:2016
Values are ptra : 1724577620    ptrb:1724577616
Address of a is 1724577616     Value of a is 2016
Address of b is 1724577620     Value of b is 0
1.Values are *ptra : 2016    *ptrb:42
Values are ptra : 1724577616    ptrb:1724577628
2.Values are *ptra : 42    *ptrb:2016
Values are ptra : 1724577628    ptrb:1724577616
Value of a is 2016

Memory Model
 

4 Comments

Thanks for the awesome explanation buddy.
0
0
Clear and great explanation crystal clear i should say
0
0
I was confused about why the addresses are not changed even after swapping them. Thanks for the explanation.
0
0
–2 votes
–2 votes
# include <stdio.h>
void mystery (int *ptra, int *ptrb) {  
     int *temp;
     *temp = *ptrb;  
     *ptrb =*ptra;
     *ptra = *temp;
}
int main () {
    int a = 2016, b=0, c= 4, d = 42;
    mystery (&a, &b);
    if (a < c)
          mystery (&c, &a);
    mystery (&a, &d);
    printf("%d\n", a);
}

 

 

o/p is 42

4 Comments

as i saw in previous comment

"We can observe this using the memory stack. First main()  is pushed onto the stack, then mystery(&a,&b) is pushed onto the stack.
Now in the mystery function the addresses gets swapped. After the end of the function ptra points at 0 and ptrb at 2016. BUT these addresses are lost as when mystery is popped out and control goes back to main."

but in this example why mystry function would affect the main function ?  why we can't observe using the memory stack? please tell me anyone thank you
0
0
what if we use something like

temp = *ptrb ?

then values inside "a " and " b " gets swapped ?
0
0
int temp instead of int *temp;
0
0
Answer:

Related questions