C Arrays

C Multidimensional Array

  • Multi-dimensional arrays are declared by providing more than one set of square [ ] brackets after the variable name in the declaration statement.
  • One dimensional arrays do not require the dimension to be given if the array is to be completely initialized.  By analogy, multi-dimensional arrays do not require the first dimension to be given if the array is to be completely initialized.  All dimensions after the first must be given in any case.
  • For two dimensional arrays, the first dimension is commonly considered to be the number of rows, and the second dimension the number of  columns.  We will use this convention when discussing two dimensional arrays.
  • Two dimensional arrays are considered by C/C++ to be an array of ( single dimensional arrays ).  For example, "int numbers[ 5 ][ 6 ]"  would refer to a single dimensional array of 5 elements, wherein each element is a single dimensional array of 6 integers.  By extension, "int numbers[ 12 ][ 5 ][ 6 ]" would refer to an array of twelve elements, each of which is a two dimensional array, and so on.
  • Another way of looking at this is that C stores two dimensional arrays by rows, with all elements of a row being stored together as a single unit.  Knowing this can sometimes lead to more efficient programs. 
  • Multidimensional arrays may be completely initialized by listing all data elements within a single pair of curly {} braces, as with single dimensional arrays.
  • It is better programming practice to enclose each row within a separate subset of curly {} braces, to make the program more readable.  This is required if any row other than the last is to be partially initialized.  When subsets of braces are used, the last item within braces is not followed by a comma, but the subsets are themselves separated by commas.
  • Multidimensional arrays may be partially initialized by not providing complete initialization data.  Individual rows of a multidimensional array may be partially initialized, provided that subset braces are used.
  • Individual data items in a multidimensional array are accessed by fully qualifying an array element.  Alternatively, a smaller dimensional array may be accessed by partially qualifying the array name.  For example, if  "data" has been declared as a three dimensional array of floats, then data[ 1 ][ 2 ][ 5 ] would refer to a float, data[ 1 ][ 2 ] would refer to a one-dimensional array of floats, and data[ 1 ] would refer to a two-dimensional array of floats.  The reasons for this and the incentive to do this relate to memory-management issues that are beyond the scope of these notes.

Sample Program Using 2-D Arrays

        /* Sample program Using 2-D Arrays */
        #include <conio.h>
        #include <stdio.h> 
        void main() {
            /* Program to add two multidimensional arrays */
            /* Written May 1995 by George P. Burdell */
            int a[ 2 ][ 3 ] = { { 5, 6, 7 }, { 10, 20, 30 } };
            int b[ 2 ][ 3 ] = { { 1, 2, 3 }, {  3,  2,  1 } };
            int sum[ 2 ][ 3 ], row, column;
            /* First the addition */
            for( row = 0; row < 2; row++ ){
                for( column = 0; column < 3; column++ ){
                    sum[ row ][ column ] =  
                        a[ row ][ column ] + b[ row ][ column ];
            /* Then print the results */
            printf( "The sum is: \n\n" );
            for( row = 0; row < 2; row++ ) {
                for( column = 0; column < 3; column++ ){
                    printf( "\t%d",  sum[ row ][ column ] );}
                printf( '\n' );   /* at end of each row */

Please Share this page
Views : 22
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!