retagged by
831 views
4 votes
4 votes

Consider the following declaration of pointer variable $p.$

int (*p)[10][5];

If the initial value of $p$ is $1000,$ then what will be the value of $p+1?$

It is given that system has $8$ bytes of address size and $4$ bytes of integer size.

retagged by

2 Answers

4 votes
4 votes
int (*p)[10][5];

Here $p$ is a pointer to entire 2D array so $p+1$ will skip the entire 2D array.

If initial address is $1000$ and $p+1$ will skip entire 2D array i.e. $10*5 = 50$ integers. Each integer require $4B$. So $50$ int requires $50*4 = 200$ Bytes in total.

By default memory is Byte addressabe therefore each byte requires $1$ address. $200B$ will require $200$ address lines. So $p+1$ will point to $1000 + 200 = 1200$

2 votes
2 votes
$*p$ in $\text{int} (*p)[10][5]$ has higher priority due to parenthesis. So, it’s a pointer to 2d int array of dimensions [10][5].

p is given as 1000. $p + 1$ is equivalent to saying $p + 1*sizeof(*p)$. $*p$ means the type pointed to by the pointer p.

Since p points to int array of size $10*5$, $sizeof(*p) = 10*5*sizeof(int) = 10*5*4 = 200$, we have,

$p+1 = p+1*sizeof(*p) = 1000+1*200 = 1200$
Answer:

Related questions

2 votes
2 votes
3 answers
4