Multidimensional arrays can be described as "arrays of
arrays". For example, a bidimensional array can be imagined as a
bidimensional table made of elements, all of them of a same uniform data type.

jimmy represents a
bidimensional array of 3 per 5 elements of type int. The way to declare this array in C++ would be:
|
|
int |
and, for example, the way to reference the second element vertically and fourth
horizontally in an expression would be:
|
|
|

(remember that array indices always begin by zero).
Multidimensional
arrays are not limited to two indices (i.e., two dimensions). They can contain
as many indices as needed. But be careful! The amount of memory needed for an
array rapidly increases with each dimension. For example:
|
|
char |
declares an array with a char element for each second in a
century, that is more than 3 billion chars. So this declaration would consume
more than 3 gigabytes of memory!
Multidimensional
arrays are just an abstraction for programmers, since we can obtain the same
results with a simple array just by putting a factor between its indices:
|
1 |
int int |
With the
only difference that with multidimensional arrays the compiler remembers the
depth of each imaginary dimension for us. Take as example these two pieces of
code, with both exactly the same result. One uses a bidimensional array and the
other one uses a simple array:
|
multidimensional array |
pseudo-multidimensional array |
#define WIDTH 5#define HEIGHT 3 int jimmy [HEIGHT][WIDTH];int n,m; int main (){ for (n=0;n<HEIGHT;n++) for (m=0;m<WIDTH;m++) { jimmy[n][m]=(n+1)*(m+1); } return 0;} |
#define WIDTH 5#define HEIGHT 3 int jimmy [HEIGHT * WIDTH];int n,m; int main (){ for (n=0;n<HEIGHT;n++) for (m=0;m<WIDTH;m++) { jimmy[n*WIDTH+m]=(n+1)*(m+1);} return 0;} |
None of the two source codes above produce any output on the screen, but both
assign values to the memory block called jimmy in the following way:

We have used "defined constants" (#define) to simplify possible future modifications of the
program. For example, in case that we decided to enlarge the array to a height
of 4 instead of 3 it could be done simply by changing the line:
|
|
#define HEIGHT 3 |
to:
|
|
#define HEIGHT 4 |
with no need to make any other modifications to the program.
At some moment we may need to pass
an array to a function as a parameter. In C++ it is not possible to pass a complete
block of memory by value as a parameter to a function, but we are allowed to
pass its address. In practice this has almost the same effect and it is a much
faster and more efficient operation.
In order to accept arrays as
parameters the only thing that we have to do when declaring the function is to
specify in its parameters the element type of the array, an identifier and a
pair of void brackets []. For
example, the following function:
|
|
void |
accepts a parameter of type "array of int" called arg. In order to pass to this function an array declared
as:
|
|
int |
it would be enough to write a call like this:
|
|
|
Here you have a complete example:
|
1 |
// arrays as parameters#include <iostream>using void
int
|
5 10 152 4 6 8 10 |
As you
can see, the first parameter (int arg[])
accepts any array whose elements are of type int, whatever its length. For that reason we have
included a second parameter that tells the function the length of each array
that we pass to it as its first parameter. This allows the for loop that prints out the array to know
the range to iterate in the passed array without going out of range.
In a
function declaration it is also possible to include multidimensional arrays.
The format for a tridimensional array parameter is:
|
|
|
for example, a function with a multidimensional array as argument could be:
|
|
Void |
Notice
that the first brackets [] are
left blank while the following ones are not. This is so because the compiler
must be able to determine within the function which is the depth of each
additional dimension.
Arrays,
both simple or multidimensional, passed as function parameters are a quite
common source of errors for novice programmers. I recommend the reading of the
chapter about Pointers for a better understanding on how arrays operate.