In C if format of 2d Array is arr[n][0],then is this a one dimensional array? How it is stored in memory,if number of columns is 0?
-
1Could you show us where this is used? – Arc676 Feb 21 '16 at 08:46
-
In order to get the "right" answer for you, you have to put your question in context, ie shows it in code. – artm Feb 21 '16 at 09:21
-
If the number of columns is 0, then it is empty. It's not legal C. What else would you expect? If the number of columns were 1, then your question would at least make sense. – Tom Karzes Feb 21 '16 at 09:38
3 Answers
If you mean an array declaration like
T arr[n][0];
where T
is a type specifier then it is invalid.
Accoriding to the C Standard (6.7.6.2 Array declarators)
1 In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero....

- 301,070
- 26
- 186
- 335
As an additional answer:
Two-dimentional arrays are laid out flat in memory, it's a linear collection of addresses.
If you visualise a 4X4 array like this:
[0][0][0][0]
[1][1][1][1]
[2][2][2][2]
[3][3][3][3]
The array is laid out in memory like this:
[0][0][0][0][1][1][1][1][2][2][2][2][3][3][3][3]
For example when declaring a function that takes a two-dimentional array as argument, you always need to provide the size for the second part of the array.
You can provide both, or just the second: int func(int array[4][4])
or int func(int array[0][4])
But you can't omit both or provide just the first size.
Why? Because only certain sizes are required to do the correct pointer arithmetic:
For example, in order to use array[3][2]
we go 3 rows down and 2 across, each row being 4 integers
wide = 4 * 3
integers slots and then add 2
more integers slots to get to the 3rd element.
array[3][2]
turns into pointer arithmetic *(array + 3 * <width of array> + 2)
:
array[3][2] == *((int*)array + 3 * 4 + 2)
Check this answer for why (int*)
is there.

- 1
- 1

- 10,685
- 6
- 35
- 62
Secretly I think it's stored as a one dimensional array in memory. The 2D is for us to use not for the computer.
As for the question regarding to arr[n][0], the number of columns is not 0. arr[n][0] simply means you are trying to access the element at nth row, 0th column. Basically any element in the first row. You still have a 2D array not a 1D array.

- 1,057
- 12
- 32