in Programming in C edited by
17,775 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
17.8k views

4 Comments

nice explation :D
0
0

 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

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

4 Comments

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

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true