Multidimensional Array in C Programming

This C Tutorial Explains Multidimensional Array in C Programming with Example(s).

An array with more than one dimension is called a multidimensional array. Consider an example below,

    int hawks[5][3];        /* a two dimensional array */
    int pieces[2][2][3];    /* a three dimensional array */	
    int overs[5][5][5][5];  /* a four dimensional array */

and so on. Let’s first explore how they are read,

    int hawks[5][3];

‘hawks’ is an array of 5 something, each of which is an array of 3 integers, i.e. ‘hawks’ is an array of 5 arrays of 3 integers. Similarly, ‘pieces’ is an array of 2 arrays of two arrays of 3 integers.

O key! We now discuss how multi-dimensional arrays are initialized? let’s take, first, a 2-dim array below,

advertisement
advertisement
    int hawks[5][3] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
                              /* 15 integers are in hawks */

Since ‘hawks’ contains 5 arrays of 3 integers each, i.e. ‘hawks’ contains 15 integers. These are allocated in continuous locations in memory. Since ‘hawks’ contains 5 arrays of 3 integers, we can rewrite the initialization as

    int hawks[5][3] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}}; 
    /* initializer list contains 5 elements, each contains 3 integers */

Now, we consider a 3-dim array and try to initialize it,

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

‘pieces’ is an array of 2 arrays of 2 arrays of 3 integers. In fact, ‘pieces’ contains 12 integers. We can also remove the inner sets of curly braces and initialization comes to shape as:

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

However, this form of initialization isn’t easy in order to determine which element belongs to which dimension. Further, if we omit some trailing initializers from the list, we shall still have to type in long list. For ex.

    int pieces[2][2][3] = {1,2,3,4,5,6,7,8,9};
    /* three trailing initializers are omitted */

Also, in such initialization, we can omit initializers only from the end of list.

advertisement

Once more, we consider pieces[2][2][3] and initialize it using inner sets of curly braces as below,

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

and observe that pieces has two arrays

    1. {{1,2,3},{4,5,6}}
    2. {{7,8,9},{10,11,12}}

Each array further contains two arrays of 3 integers each. With such initialization, don’t we find it easy to determine which element belongs to which dimension and also we can now omit trailing initializers from each list. Let’s try this out right now, below,

advertisement
    int pieces[2][2][3] = {
                           {{1},{4}},
                           {{7},{10}}
			  };

How easy it’s become to omit trailing initializers from each list and therefore, it’s become handy to type long list of initializers when only few initializers are given.

Remember that such initialization is very useful in writing multi-dimensional arrays having very few elements.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.