2-Dimensional Array

  • A two-dimensional array is in essence, an array of one-dimensional arrays.
  • They simulate the same architectural definition as one – dimensional arrays, except that a separate pair of square brackets is required for each subscript.
  • To declare a two-dimensional integer array of size x,y you would write something as follows:
    datatype arrayName [ x ][ y ];
  • Initializing Two-Dimensional Arrays:
    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 */
    };

    The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to previous

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

    Accessing Two-Dimensional Array Elements:
    An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array.

    int val = a[2][3];

    The above statement will take 4th element from the 3rd row of the array. You can verify it in the above diagram. Let us
    check below program where we have used nested loop to handle a two dimensional array: