Lecture Notes for Introduction to Computer Science II

26 June 2001 - Multi-Dimensional Arrays


  1. array element types can be anything, even other arrays

    1. multi-dimensional arrays, vectors, matrices, multiply-subscripted arrays

    2. each dimension has a size

    3. for example, a 2-dimensional n x m array is a

  2. declarations

    1. a semi-special syntax - int m[3][3]

    2. a minimum of 12 dimensions

  3. accessing

    1. a sequence of square brackets - m[i][j]

    2. don't slip and use one bracket - m[i, j] works, probably wrong

  4. initialization

    1. remember the vector of vectors

    2. a full initialization list

      1. int a[2][3] = { { 1,2,3 },{ 4,5,6} }

    3. row initializer lists can be partial - zero fill

      1. int a[2][3] = { { 1 },{ 4,5 } }

    4. row initializer lists can missing - more zero fill

      1. int a[2][3] = { { 1,1,1 } }

    5. element-by-element initialization

      1. int a[2][3] = { 1,2,3 }

      2. elements initialized in memory-layout order; zero fill

    6. element-by-element and row-by-row can't be mixed in the same initializer list

  5. function parameter declarations

    1. array parameters don't need sizes - not quite

    2. only the first dimension doesn't need a size; the rest do

    3. example - int f(int a[][3], int a_size) { . . . }

    4. the size is still not part of the array; the compiler doesn't check

      1. int a[5][5]; f(a); compiles, doesn't work


This page last modified on 25 June 2001.