Lecture Notes for Introduction to Computer Science II
26 June 2001 - Multi-Dimensional Arrays
- array element types can be anything, even other arrays
- multi-dimensional arrays, vectors, matrices, multiply-subscripted
arrays
- each dimension has a size
- for example, a 2-dimensional n x m array is a
- declarations
- a semi-special syntax -
int m[3][3]
- a minimum of 12 dimensions
- accessing
- a sequence of square brackets -
m[i][j]
- don't slip and use one bracket -
m[i, j]
works, probably wrong
- initialization
- remember the vector of vectors
- a full initialization list
-
int a[2][3] = { { 1,2,3 },{ 4,5,6} }
- row initializer lists can be partial - zero fill
-
int a[2][3] = { { 1 },{ 4,5 } }
- row initializer lists can missing - more zero fill
-
int a[2][3] = { { 1,1,1 } }
- element-by-element initialization
-
int a[2][3] = { 1,2,3 }
- elements initialized in memory-layout order; zero fill
- element-by-element and row-by-row can't be mixed in the same
initializer list
- function parameter declarations
- array parameters don't need sizes - not quite
- only the first dimension doesn't need a size; the rest do
- example -
int f(int a[][3], int a_size) { . . . }
- the size is still not part of the array; the compiler doesn't check
-
int a[5][5]; f(a);
compiles, doesn't work
This page last modified on 25 June 2001.