C Programming Reference >> C Programming Reference Question Bank >>
19/07/07 - Views - Ratings :    3.5 of 5 / 16 Votes
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 -
| pooja
|
where is struct "node" defined here?
|
| pooja
|
where is struct "node" defined here?
|
| siri
|
Can we intialize the structure as Array ?
|
| Sachin
|
Yes structure can be initilized as arrays eg if student is structure then student[some no]; declares student as structure
|
| maddoxx
|
Remove (int *) and complie
Output is still 3
|
|