Log In
4 votes

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    B[10];
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
edited by
What is correct answer

Option A or C????

3 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)$

@srestha Not able to understand the second part can you please explain it again? Thank you


@Pratyush Priyam Kuan

which portion ??


@srestha how 

((int)((int)&(p→c))−((int)p)=2 ? 

1 vote

$p$ is a pointer of type "S" which points to the element at index $4$ of some array $B$.


$q$ is a pointer of type "S" which points to the element at index $5$ of some array $B$.


$p$ and $q$ are neighbours. By pointer arithmetic, $q-p = 1$.

Now, union is of size $2$ Bytes. Becuase the size of a union is the size of the largest field in it. (Short = $2$ Bytes, Char = $1$ Byte)

Hence, overall struct is of size $3$ Bytes.

$2$ Bytes for Union, and $1$ Byte for the unsigned char $c$.


Given that:

/* structure S takes 32-bits */

Means, struct takes $4$ Bytes. That is because of Byte alignment. See this.

So, struct looks like this:

Union Char C Padding
2 Bytes 1 Byte 1 Byte


Because $B$ is declared as an array of $10$ elements, whose elements are of type $S$,

Hence, the overall size of array $B$ is $10*4=40$ Bytes


Suppose the starting address of the $4th$ index of $B$ is $5000$.

$p→c$ would point to $5002$

$p$ would point to $5000$.


Since both $p→c$ and $p$ are typecasted as int, and the size of an int is at least 2 Bytes, hence

$((int ) \& (p→c))–((int)p) = (5002 - 5000) / 2 = 1$


Option A is correct.
0 votes
Option (C):(1,2)

Related questions

1 vote
1 answer
Consider the following C++ function f(): unsigned int f(unsigned int n) { unsigned int b=0; while (n) { b+=n & 1; n>>1; } return b; } The function f() returns the int that represents the ____P____ in the binary representation of positive integer n, where P is number of $0$’s number of bits number of consecutive $1$’s number of $1$’s
asked Jul 2, 2019 in Object Oriented Programming Arjun 541 views
3 votes
1 answer
Consider the following pseudo-code fragment in which an invariant for the loop is $m ^*x^k=p^n$ and $k \geq 0$ (here, $p$ and $n$ are integer variable that have been initialized): /* Pre-conditions : $p \geq 1 \wedge n \geq 0$ */ /* Assume that overflow never occurs */ int $x=p$; ... $x=p^n$ $m=p^n$ $p=x^n$ $p=m^n$
asked Jul 2, 2019 in Programming Arjun 575 views
8 votes
3 answers
Consider the poset $( \{3,5,9,15,24,45 \}, \mid).$ Which of the following is correct for the given poset ? There exist a greatest element and a least element There exist a greatest element but not a least element There exist a least element but not a greatest element There does not exist a greatest element and a least element
asked Jul 2, 2019 in Set Theory & Algebra Arjun 1.9k views
2 votes
2 answers
How many ways are there to place $8$ indistinguishable balls into four distinguishable bins? $70$ $165$ $^8C_4$ $^8P_4$
asked Jul 2, 2019 in Combinatory Arjun 1.4k views