C Quiz – 110 | Question 5

In C, 1D array of int can be defined as follows and both are correct.

filter_none

edit
close

play_arrow

link
brightness_4
code

int array1D[4] = {1,2,3,4};
int array1D[] = {1,2,3,4};

chevron_right


But given the following definitions (along-with initialization) of 2D arrays

filter_none

edit
close

play_arrow

link
brightness_4
code

int array2D[2][4] = {1,2,3,4,5,6,7,8}; /* (i) */
int array2D[][4] = {1,2,3,4,5,6,7,8}; /* (ii) */
int array2D[2][] = {1,2,3,4,5,6,7,8}; /* (iii) */
int array2D[][] = {1,2,3,4,5,6,7,8}; /* (iv) */

chevron_right


Pick the correct statements.
(A) Only (i) is correct.
(B) Only (i) and (ii) are correct.
(C) Only (i), (ii) and (iii) are correct.
(D) All (i), (ii), (iii) and (iv) are correct.


Answer: (B)

Explanation: First of all, C language doesn’t provide any true support for 2D array or multidimensional arrays. A 2D array is simulated via 1D array of arrays. So a 2D array of int is actually a 1D array of array of int. Another important point is that array size can be derived from its initialization but that’s applicable for first dimension only. It means that 2D array need to have an explicit size of 2nd dimension. Similarly, for a 3D array, 2nd and 3rd dimensions need to have explicit size. That’s why only (i) and (ii) are correct. But array2D[2][] and array2D[][] are of incomplete type because their complete size can’t derived even from the initialization.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.