#include<stdio.h>
int i=0, j=0;
void main(){
int a[3][5]={1,2,{3,4,6,8},{5,8,9},10,{11,12,13,14},{21,22},23,9,8,7,6,5,4,3};//Array Initialisation
for(i=0; i<3; i++){
for(j=0; j<5; j++){
printf("\na[%d][%d]:%d\n", i, j, a[i][j]);//Array Printing
}
}
}
/*The above code initialises the array with some logic that I'm unable to understand. How are the set elements treated? Please explain */
Asked
Active
Viewed 52 times
-5
-
The array definition looks wrong given the bounds `[3][5]`. – Tim Biegeleisen Jan 22 '16 at 08:11
-
@boson see example [here](http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm). In C arrays the length of every row should be the same... – urban Jan 22 '16 at 08:14
-
Conforming to the standard you should use int main(void) – Claudio Cortese Jan 22 '16 at 08:14
-
@ClaudioCortese Not correct. [See this](http://stackoverflow.com/a/31263079/584518). – Lundin Jan 22 '16 at 08:24
-
@boson You need to get a decent compiler and enable its warnings. You should be getting lots of compiler warnings for that initializer list. – Lundin Jan 22 '16 at 08:25
-
@Lundin are you referring to int main(int argc, char *argv) ? – Claudio Cortese Jan 22 '16 at 08:36
-
@ClaudioCortese No I'm referring to `void main ()` which is perfectly standard compliant on: freestanding systems C90, C99, C11 and hosted systems C99, C11. Given that the compiler documents the use of the implementation-defined form `void main()`. If the compiler does not mention such an implementation of main, then the code invokes undefined behavior. For example, [here](https://msdn.microsoft.com/en-us/library/6wd819wh.aspx) is the documentation for the Visual Studio compiler which explains how void main() is allowed on that compiler. – Lundin Jan 22 '16 at 08:42
-
Look at the warnings you get when you compile the program. – August Karlstrom Jan 22 '16 at 08:53
1 Answers
2
You don't get the point of bi-dimensional array in C:
int A[2][3]
is the declaration of a bi-dimensional array of 6 integers elements with two rows and three columns. This is always true, the number in the first square brackets stands for rows, instead the number in the second square brackets stands for columns. To initialize a bi-dimensional array you need to know these things:
int a[3][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
As you can see there are three curly brackets (the rows), inside these three curly brackets there are 5 numbers (the columns).

Claudio Cortese
- 1,372
- 2
- 10
- 21