Dark Mode

208 views

1 vote

Consider the following declarations of a few variables.

int (*p1)[3]; int (*p2)[3]; int (*t1)[2]; int (*t2)[2];

Let the initial values of $p1, p2, t2$ and $t1$ are $1000, 1036, 2000$ and $2040$ respectively.

Consider two statements below-

- $\text{S1}:$ Value of $p2-p1$ is $3$
- $\text{S2}:$ Value of $t1-t2$ is $5$

Choose the correct option-

- $\text{S1}$ is correct but $\text{S2}$ is incorrect
- $\text{S2}$ is correct but $\text{S1}$ is incorrect
- Both are correct
- Both are incorrect

3 votes

**Answer: C**

Since pointer difference is asked so all the pointers $p1$, $p2$ are pointing to the same array similarly pointer $t1$, $t2$ is pointing to the same array.

int (*p1)[3]; // p1 is pointing to the array of 3 integers int (*p2)[3]; // p2 is pointing to the array of 3 integers

$p1$ address is $1000$, $p2$ address is $1036$

$p2-p1 = \frac {1036-1000}{4*3} = \frac{36}{12} = 3$ *(dividing by 4 because each integer is of 4B and by 3 because each pointer is pointing *

* to an array of 3 integers)*

int (*t1)[2]; //t1 is pointing to an array of 2 integers int (*t2)[2]; //t2 is pointing to an array of 2 integers

$t2$ address is $2000$, $t1$ address is $2040$

$t1-t2 = \frac{2040-2000}{4*2} = \frac{40}{8} = 5$