2
int b[3][2] = { {0, 1}, {2, 3}, {4, 5} };
int (*bpp)[2] = b;
int *bp = b[0];

At the above code: Is *bpp a pointer to a two-dimensional array? Or an array of pointers with the length of 2? Why is *bpp surrounded with parenthesis? Is there a difference between *bpp[2] and (*bpp)[2] ?

Meantime, in the following code: (Changing the dimension of the array)

int i[4] = { 1, 2, 3, 4 };
int (*ap)[2] = (int(*)[2])i;

The second line is very confusing to me, especially the typecasting (int(*)[2]), what data type is it exactly casting to?

Thank you ^^

1 Answer 1

3

bpp is a pointer to an array of two int. *bpp is an array of two int. int *bpp[2] would declare bpp as an array of two pointers to int (this parentheses make it be a pointer to an array of two int).

(int(*)[2]) is a cast to a pointer to an array of two int.

These can be read by considering the "declaration follows use" rule (combined with knowledge of operator precedence):

  dereference (so bpp is a pointer)
     |
     v
int (*bpp)[2]
 ^         ^
 |         |
 |  array index (so the thing that bpp points to is an array)
 |
 the thing on the left is the final type... here it is int,
 so the array is an array of int
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.