Tuesday 15 January 2019

Multidimensional Arrays

#Tutorial4#

In the previous tutorials on Arrays, we covered, well, arrays and how they work. The arrays we looked at were all one-dimensional, but C can create and use multi-dimensional arrays. Here is the general form of a multidimensional array declaration:


type name[size1][size2]...[sizeN];
 
For example, here's a basic one for you to look at -


int foo[1][2][3];
 
or maybe this one -


char vowels[1][5] = {
    {'a', 'e', 'i', 'o', 'u'}
};
 

Two-dimensional Arrays

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. A two-dimensional array can 
be considered as a table which will have [ x ] number of rows and [ y ] 
number of columns. A two-dimensional array a, which contains three rows 
and four columns can be shown and thought about like this − 
 
Table 1A 
In this sense, every element in the array a is identified by an element name in 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 -


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

No comments:

Post a Comment

Note: only a member of this blog may post a comment.