in Programming in C retagged by
860 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.

in Programming in C retagged by
860 views

4 Comments

edited by

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

3
3
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.
2
2
What is the significance of address size in general? I mean I know here is just given to confuse us.
0
0

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$

by
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