in Programming edited by
244 views
1 vote
1 vote

Consider the following declarations of variables in a system having $4$ bytes for integers.

int a[5][4];
int (*p1)[2];
int (*p2)[2];
int (*t1)[4];
int (*t2)[4];

Suppose array a has base address of $1000$ and initial values of $p1, p2, t1$ and $t2$ are $1000, 1048, 1016$ and $1064$ respectively. What will be the value of $p2-p1$ and $t2-t1$ respectively?

  1. $6 \;3$
  2. $48\; 24 $
  3. $12\; 6$
  4. $24\; 12$
in Programming edited by
244 views

1 Answer

4 votes
4 votes

p1 and p2 both are pointer to an integer array of size 2. p1 = 1000 and p2 = 1048

Thus, p2 - p1 = (1048 – 1000) / sizeof(*p1) = 48 / (2*4) = 6

t1 and t2 both are pointer to an integer array of size 4. t1 = 1016 and t2 = 1064

Thus, t2 - t3 = (1064 – 1016) / sizeof(*t1) = 48 / (4*4) = 3

https://onlinegdb.com/UbKb88Ao8 refer this to see execution/output.

Answer :- A. 6 3

edited by

3 Comments

edited by
(pointer – pointer) returns long int.
1
1

@shishir__roy What does the following lines mean?    

    int (*p1)[2] = (int (*)[2])1000;
    int (*p2)[2] = (int (*)[2])1048;
    int (*t1)[4] = (int (*)[4])1016;
    int (*t2)[4] = (int (*)[4])1064;
    

0
0
int (*p1)[2] = (int (*)[2]) 1000;

Here -

p1 is a pointer to int array of size 2 ie p1 can hold an address of an int array of size 2.

1000 is given in question, it is an address.

(int (*)[2]) this is used to explicitly typecast the address ie we're saying to compiler that whatever follows is an address of type int (*)[2] (address of an int array of size 2)
2
2
Answer:

Related questions