edited by
279 views
1 votes
1 votes

What will be the output of the below program ?

#include <stdio.h>
int main()
{
struct node
{
int x;
int y;
int z;
};
struct node s = { 3, 5, 6 };
struct node *pt = &s;
printf("%d\n", *((int*)pt+1));
return 0;
}
  1. 5
  2. 3
  3. 7
  4. 6
edited by

1 Answer

2 votes
2 votes

Answer A 

struct node s = { 3, 5, 6 };
struct node *pt = &s;
printf("%d\n", *((int*)pt+1));

in last line we are type casting the pt pointer to type (int *) . Now after casting , pt+1 means pt +sizeof(int *)x1.

we know sizeof(int *)=4 and pt basically point the starting address of the struct node .Also we know in struct , elements are stored in contiguous memory location similar to an array.

Hence pt+1 will point to the second element of the structure. so redirection of same yield the second element ie 5

Answer:

Related questions

3 votes
3 votes
1 answer
1
Bikram asked May 14, 2017
392 views
Assume that $a[4]$ is a one-dimensional array of $4$ elements, $p$ is a pointer variable and $p = a$ is performed. Now, which among these expressions is illegal?$p == a[0...
0 votes
0 votes
1 answer
2
Bikram asked May 14, 2017
351 views
Spot the error(s) in this code snippet :int n=2; // Line 1 switch(n) { case 1.5: printf( "gate"); break; case 2: printf( "overflow"); break; case 'A': printf("gateoverflo...
0 votes
0 votes
1 answer
3
Bikram asked May 14, 2017
198 views
#include<stdio.h int K = 10; int main() { foo(); foo(); return 0; } int foo() { static int k= 1; printf("%d ",k); k++; return 0; }What is the output of the above code sni...
0 votes
0 votes
1 answer
4
Bikram asked May 14, 2017
202 views
What is the output of the following program? int fun (int z) { if( z==0 || z==1 ) return 1; else return fun(z-1) ; } int main() { int y; y=fun(8); printf(“%d”...