The Gateway to Computer Science Excellence
+2 votes
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)$
in Programming by Veteran (424k points)
edited by | 388 views

2 Answers

+2 votes
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)
0 votes
Option (C):(1,2)
by (289 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,648 questions
56,422 answers
195,194 comments
99,823 users