388 views

Consider the following C-code fragment running on a $32$-bit $X86$ machine:

typedef struct {
union {
unsigned char a;
unsigned short b;
} U;
unsigned char c;
}S;

S    B[10];
S*p=&B[4];
S*q=&B[5];
p → U.b=0x1234;
/* structure S takes 32-bits */


If M is the value of $q-p$ and $N$ is the value of $((\text{int & } ( p \rightarrow c)) – ((\text{int})p)$, then $(M,N)$ is

1. $(1,1)$
2. $(3,2)$
3. $(1,2)$
4. $(4,4)$

edited | 388 views

We have to find $q-p$

$q$ is nothing but address of $B\left [ 5 \right ]$, say it is $1010$

and $B\left [ 4 \right ]$ is $1008.$

Then $q-p=\frac{1010-1008}{size of(int)}=1$

Now, we have to find $((int)$&$(p\rightarrow c))-((int)p)$.

Here $c$ is $char$ and typecasted to $int.$

And, $((int)p)$ is 'short int' type , it's size '4B'

So, $((int)$&$(p\rightarrow c))-((int)p)=2$

Answer will be $(3)$
by Veteran (117k points)
Option (C):(1,2)
by (289 points)

+1 vote