12.12 Multidimensional Arrays

A multidimensional array is simply an array variable whose values are themselves arrays. The important point is that multidimensional arrays have no special features; an array can store variables of every type: integers, strings, or other arrays. Everything you learned about arrays (notation etc.) also applies to multidimensional arrays.

Consider the following definition:

foo [ ] = {
  { "aaa", "bbb", "ccc" },
  { 1, 2, 3 }
};

foo [ ] is an array consisting in two other arrays so:

foo [0]

is the array { "aaa", "bbb", "ccc" }

foo [1]

is the array { 1, 2, 3 }

foo [0][0]

is the string "aaa"

foo [1][2]

is the number 3

x [ ] = foo [0]

assigns array { "aaa", "bbb", "ccc" } to variable x

An array can also sore arrays of different sizes, or both scalars and arrays; the following definition is correct:

bar [ ] = {
            1,
            "foo",
            { "aaa", "bbb" },
            { 1, 2, 3, 4, 5 },
          };
bar [0]

is the scalar integer 1

bar [1]

is the scalar string "foo"

bar [2]

is an array consisting in 2 elements

bar [3]

is an array consisting in 5 elements

This manual can be downloaded from http://www.g-cows.org/.