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

19/07/07 - 510 Views - No Ratings Yet

Level - Begineer
      

Question -

What will be  the output of the following code and why?
#include <stdio.h>

struct data    
   {       
      int a;      
      int b;      
      int c;          
   };    
 
int main()  
{    
  
 struct node  s= { 3, 5,6 };     

struct node *pt = &s;    
printf("Ans : %d" ,  *(int*)pt);  

return 0;
}  
Answer -
Ans : 3
Explanation -

In a structure, all its elements are allocated storage in declaration order. So when the pointer to structure is type casted into a pointer of type of first element of declaration and its value is displayed then value shown will be the value of that first variable. So value of a ie. 3 will be displayed in output of the c code snippet above.

Reader Comments -

Author Comments
Add Comments 


Name :    
Reply :   


Rating :

Code :
Code

 

 
© 2006 cencyclopedia.com