Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

10.1 Two-Dimensional Arrays

Some problems require the use of a two-dimensional array, which organizes data into rows and columns similar to a table or grid as illustrated in Figure 10.1.1. The individual elements are accessed by specifying two indices, one for the row and one for the column.

Figure 10.1.1. A sample 2-D array consisting of 4 rows and 5 columns.

The C programming language only provides for the explicit declaration of static 2-D arrays, although, dynamic 2-D arrays are possible. To create a 2-D array, you must specify four pieces of information:

  • the name of the array
  • the number of rows and columns in the array,
  • the type of data stored in each element.

The total number of elements in the 2-D array will be the number of rows multiplied by the number of columns (nrows x ncols). For example, the declaration:

  1.   int table[3][5];

creates a 2-D array with the name table that contains a total of 15 elements arranged into 3 rows of 5 columns each, all of which store integer values.

As with 1-D arrays, a two-dimensional array can only store values of a specific data type and once created, you cannot change the size of the array.

Syntax
Defining a 2-D Array
type name[nrows][ncols];
name

The name must be a valid identifier that specifies the name of the 2-D array variable.

type

The specified type can be any valid data type which is used to indicate the type of data stored in each element of the 2-D array.

nrows

Indicates the number of rows contained in the 2-D array. This value must be known at compile time. Thus, it must be either a literal value or the value of a constant variable.

ncols

Indicates the number of columns contained in the 2-D array. This value must be known at compile time. Thus, it must be either a literal value or the value of a constant variable.

Note that the elements of the array are not initialized to any given value, but will contain the values that are currently stored in the memory location where the array is created. A 2-D array can be initialize when it is first created in a similar fashion to 1-D arrays:

  1. int data[3][2] = {
  2.                    {15, 20},
  3.                    {8, 16},
  4.                    {23, 7}
  5.                  };

which results in the array

Syntax
Defining and Initializing a 2-D Array
type name[nrows][ncols] = {
                         { row-0 values },
                         { row-1 values },
                                :
                       };
The initial values of a 2-D array can be specified when it is first created. The values must be provided as a collection of sequences within a pair of curly braces ({ }).
  • Each pair of { } within the outer pair of curly braces comprises a single row of the 2-D array.
  • The rows of values are stored in the 2-D array starting with the first row and continuing in sequence.
  • If the number of values specified is less than the number of columns or the number of rows, any remaining elements are not initialized.
  • If the number of row collections is greater than the specified size in the declaration, the size of the array is increased to accommodate all of the rows.
  • If the number of values specified in any row collection is greater than the number of columns specified in the declaration, the results are unpredictable.
Question 10.1.1

Given the following array declaration

  1. const int SIZE = 12;
  2. float table[6][SIZE];

How many rows and columns does the array contain?

Select the correct answer below by clicking on the appropriate box.
  1. 6 rows and 6 columns
  2. 6 rows and 12 columns
  3. 12 rows and 6 columns
  4. can not be determined