Which of the statements below initializes a 2dimensional int
Which of the statements below initializes a 2-dimensional int array that represents the following table:
10 11 12 13
20 21 22 23
a. int[][] iArray = {{10,20},{11,21}, {12,22}, {13,23}};
b. int[][] iArray = {10, 11, 12, 13}{20,21,22,23};
c. int[2][4] iArray = {{10, 11, 12, 13}, {20,21,22,23}};
d. int[][] iArray = {{10, 11, 12, 13}, {20,21,22,23}};
Solution
OPTION : C
Explanation:
11down voteaccepted
You get this behavior, because int array [ROW][COLUMN] = {1}; does not mean \"set all items to one\". Let me try to explain how this works step by step.
The explicit, overly clear way of initializing your array would be like this:
However, C allows you to leave out some of the items in an array (or struct/union). You could for example write:
This means, initialize the first elements to 1 and 2, and the rest of the elements \"as if they had static storage duration\". There is a rule in C saying that all objects of static storage duration, that are not explicitly initialized by the programmer, must be set to zero.
So in the above example, the first row gets set to 1,2 and the next to 0,0 since we didn\'t give them any explicit values.
Next, there is a rule in C allowing lax brace style. The first example could as well be written as
although of course this is poor style, it is harder to read and understand. But this rule is convenient, because it allows us to write
which means: \"initialize the very first column in the first row to 0, and all other items as if they had static storage duration, ie set them to zero.\"
therefore, if you attempt
it means \"initialize the very first column in the first row to 1 and set all other items to zero\".
| 11down voteaccepted | You get this behavior, because int array [ROW][COLUMN] = {1}; does not mean \"set all items to one\". Let me try to explain how this works step by step. The explicit, overly clear way of initializing your array would be like this: #define ROW 2 #define COLUMN 2 int array [ROW][COLUMN] = { {0, 0}, {0, 0} }; However, C allows you to leave out some of the items in an array (or struct/union). You could for example write: int array [ROW][COLUMN] = { {1, 2} }; This means, initialize the first elements to 1 and 2, and the rest of the elements \"as if they had static storage duration\". There is a rule in C saying that all objects of static storage duration, that are not explicitly initialized by the programmer, must be set to zero. So in the above example, the first row gets set to 1,2 and the next to 0,0 since we didn\'t give them any explicit values. Next, there is a rule in C allowing lax brace style. The first example could as well be written as int array [ROW][COLUMN] = {0, 0, 0, 0}; although of course this is poor style, it is harder to read and understand. But this rule is convenient, because it allows us to write int array [ROW][COLUMN] = {0}; which means: \"initialize the very first column in the first row to 0, and all other items as if they had static storage duration, ie set them to zero.\" therefore, if you attempt int array [ROW][COLUMN] = {1}; it means \"initialize the very first column in the first row to 1 and set all other items to zero\". |