C Programming Reference
 
C Programming Reference >> C  Programming Reference Question Bank >>

19/07/07 - 361 Views - Ratings :   3 of 5 / 1 Votes

Level - Beginner
      

Question -


What will be  the output of the following code and why?
#include <stdio.h>
void f1(int *, int); void f2(int *, int); void(*p[2]) ( int *, int); int main() { int a; int b; p[0] = f1; p[1] = f2; a=3; b=5; p[0](&a , b); printf("%d\t %d\t" , a ,b); p[1](&a , b); printf("%d\t %d\t" , a ,b); } void f1( int* p , int q) { int tmp; tmp =*p; *p = q; q= tmp; } void f2( int* p , int q) { int tmp; tmp =*p; *p = q; q= tmp; }
Answer -
 5 5 5 5
Explanation -

First thing to note is p in above c code is an array of pointer to a function.

Second thing to observe is f1 and f2 are exactly alike in all aspects.

Third thing to see is that a is passed by refrence and b is passed by value.

And finally since value of a is passed by refrence and that of b is passed by value, value of a can be modified to b while actual value of b cannot be modified to a. So all we get is 5 for both the variables every time.

Reader Comments -

Author Comments
Add Comments 


Name :    
Reply :   


Rating :

Code :
Code

 

 
© 2006 cencyclopedia.com