4/26/2018 - 6:56 AM

Multidimensional Arrays

Mulitdimensional Arrays. Here is the general form of a multidimensional array declaration - type name[size1][size2]...[sizeN];

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is pretty much a list of one-dimensional arrays. To declare a two-dimensional integer array of size [ x ][ y ], you would write something like this - type arrayName [x][y];

Where type can be any C data type (int, char, long, long long, double, etc.) and arrayName will be a valid C identifier, or variable.

In this sense, every element in the array a is identified by an element name of the form a[i][j], where 'a' is the name of the array, and 'i' and 'j' are the indexes that uniquely identify, or show, each element in 'a'.

And honestly, you really don't have to put in a [ x ] value really, because if you did something like this -

the compiler would already know that there is two "dimensions" you could say, but, you need need NEED a [ y ] value!! The compiler may be smart, but it will not know how many integers, characters, floats, whatever you're using you have in the dimensions. Keep that in mind.

Multidimensional arrays may be used by specifying bracketed[] values for each row. Below is an array with 3 rows and each row has 4 columns. To make it easier, you can forget the 3 and keep it blank, it'll still work.

The inside braces, which indicates the wanted row, are optional. The following initialization is the same to the previous example


int foo[1][2][3];

char vowels[1][5] = {
    {'a', 'e', 'i', 'o', '\0'}

char vowels[][5] = {
    {'A', 'E', 'I', 'O', 'U'},
    {'a', 'e', 'i', 'o', NULL}

int a[3][4] = {  
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};