retagged by
20,172 views
42 votes
42 votes

Consider the following C program:

#include<stdio.h>
struct Ournode{
    char x, y, z;
};
int main() {
    struct Ournode p={'1', '0', 'a'+2};
    struct Ournode *q=&p;
    printf("%c, %c", *((char*)q+1), *((char*)q+2));
    return 0;
}

The output of this program is:

  1. 0, c
  2. 0, a+2
  3. '0', 'a+2'
  4. '0', 'c'
retagged by

9 Answers

6 votes
6 votes
Answer is: (A) 0,c

char x = 'a' + 2 (ASCII value of a + 2  , so c will be stored)

So, p={'1','0',c};

*((char*)q+1) == p[1]  // q will be character pointer and point to p, which will be incremented by 1. Hence p[1]
*((char*)q+2) == p[2]; // q will be character pointer and point to p, which will be incremented by 2. Hence p[2]
4 votes
4 votes

'a' + 2 will give 'c'

Ournode p={'1', '0', 'c'}

$*((char*)q+1) \Rightarrow 0$

$*((char*)q+2) \Rightarrow c$

1 votes
1 votes

 p = {'1','0','c'}

q is a pointer of type structure node.

In –

printf("%c, %c", *((char*)q+1), *((char*)q+2));

First we type cast  q to char * meaning q is now a pointer which can point a character or dereference only 1 byte(let, size of character is 1 byte).

therefore, 
∗((char∗)q+1) == p[1]

∗((char∗)q+2) == p[2]

Therefore answer is 0, c.

Answer:

Related questions

23 votes
23 votes
5 answers
3