in Programming recategorized by
277 views
3 votes
3 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.

in Programming recategorized by
277 views

4 Comments

$p$ is a pointer that points to an array of int of size [10][5] and $p$ points to address $1000$.

$p+1 = p + 1 * $sizeof( int [10][5] ).

$p+1 = 1000 + 1 * 10 * 5 * 4$.

$p+1 = 1200$.
1
1
edited by
is there any effect of system address size 8 byte given here or it is just to confuse
1
1
edited by

@Prateek pallaw that information is not needed, it's given just to confuse students.

2
2
p point to the whole 2D array

*p points to first array in 2D array: first row

**p points to the first element.

 

That’s all you need for this question.
1
1

2 Answers

1 vote
1 vote
$*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$
0 votes
0 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$

Answer:

Related questions