Forums Register Login
Array confusion

This is from K&B book.

which of the following codes could be inserted at line 7, and still allow the code to compile? (choose four).

a) b2 [0][1] = b;
b) b [0][0] = b3;
c) b2 [1][1][0] = b [0][0];
d) b2 [1][2][0] = b;
e) b2 [0][1][0][0] = b [0][0];
f) b2 [0][1] = big;

I couldn't understand how to work it out the dimensions.
Can anyone please help.

You just need to make sure that the reference types are the same in the LHS and RHS:

And to elaborate:

byte[][][][] b;
Now, b is a 4-dimensional byte array.
This means:
b[0] will be a 3-dimensional byte array, or a reference of type byte[][][]
b[0][0] will be a 2-dimensional byte array, or a reference of type byte[][]
b[0][0][0] will be a 1-dimensional byte array, or a reference of type byte[]
b[0][0][0][0] will be a reference of type byte (you can think of it as a 0-dimensional byte array)
Thanks Ruben for your very clear explanation.

No problem!
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

This thread has been viewed 674 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jun 23, 2018 15:18:59.