Pointers to Functions

Posted on February 21st, 2008 in C by admin

Pointers to Functions Up to this point we have been discussing pointers to data
objects. C also permits the declaration of pointers to
functions. Pointers to functions have a variety of uses and some
of them will be discussed here.

Consider the following real problem. You want to write a
function that is capable of sorting virtually any collection of
data that can be stored in an array. This might be an array of
strings, or integers, or floats, or even structures. The sorting
algorithm can be the same for all. For example, it could be a
simple bubble sort algorithm, or the more complex shell or quick
sort algorithm. We’ll use a simple bubble sort for demonstration
purposes.

Sedgewick [1] has described the bubble sort using C code by
setting up a function which when passed a pointer to the array
would sort it. If we call that function bubble(), a sort program
is described by bubble_1.c, which follows:

/*——————– bubble_1.c ——————–*/

#include <stdio.h>

int arr[10] = { 3,6,1,2,3,8,4,1,7,2};

void bubble(int a[], int N);

int main(void)
{
int i;
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
bubble(arr,10);
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
return 0;
}

void bubble(int a[], int N)
{
int i, j, t;
for(i = N-1; i >= 0; i–)
for(j = 1; j <= i; j++)
if(a[j-1] > a[j])
{
t = a[j-1];
a[j-1] = a[j];
a[j] = t;
}
}
/*———————- end bubble_1.c ———————–*/

The bubble sort is one of the simpler sorts. The algorithm scans
the array from the second to the last element comparing each
element with the one which precedes it. If the one that precedes
it is larger than the current element, the two are swapped so the
larger one is closer to the end of the array. On the first pass,
this results in the largest element ending up at the end of the
array. The array is now limited to all elements except the last
and the process repeated. This puts the next largest element at
a point preceding the largest element. The process is repeated
for a number of times equal to the number of elements minus 1.
The end result is a sorted array.

Here our function is designed to sort an array of integers.
Thus in line 1 we are comparing integers and in lines 2 through 4
we are using temporary integer storage to store integers. What
we want to do now is see if we can convert this code so we can
use any data type, i.e. not be restricted to integers.

At the same time we don’t want to have to analyze our
algorithm and the code associated with it each time we use it.
We start by removing the comparison from within the function
bubble() so as to make it relatively easy to modify the
comparison function without having to re-write portions related
the actual algorithm. This results in bubble_2.c:

/*———————- bubble_2.c ————————-*/
/* Separating the comparison function */

#include <stdio.h>

int arr[10] = { 3,6,1,2,3,8,4,1,7,2};

void bubble(int a[], int N);
int compare(int m, int n);

int main(void)
{
int i;
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
bubble(arr,10);
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
return 0;
}

void bubble(int a[], int N)
{
int i, j, t;
for(i = N-1; i >= 0; i–)
for(j = 1; j <= i; j++)
if (compare(a[j-1], a[j]))
{
t = a[j-1];
a[j-1] = a[j];
a[j] = t;
}
}

int compare(int m, int n)
{
return (m > n);
}
/*——————— end of bubble_2.c ———————–*/

If our goal is to make our sort routine data type independent,
one way of doing this is to use pointers to type void to point to
the data instead of using the integer data type. As a start in
that direction let’s modify a few things in the above so that
pointers can be used. To begin with, we’ll stick with pointers
to type integer.

/*———————– bubble_3.c ————————-*/
#include <stdio.h>

int arr[10] = { 3,6,1,2,3,8,4,1,7,2};

void bubble(int *p, int N);
int compare(int *m, int *n);

int main(void)
{
int i;
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
bubble(arr,10);
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
return 0;
}

void bubble(int *p, int N)
{
int i, j, t;
for(i = N-1; i >= 0; i–)
for(j = 1; j <= i; j++)
if (compare(&p[j-1], &p[j]))
{
t = p[j-1];
p[j-1] = p[j];
p[j] = t;
}
}

int compare(int *m, int *n)
{
return (*m > *n);
}
/*—————— end of bubble3.c ————————-*/

Note the changes. We are now passing a pointer to an integer (or
array of integers) to bubble(). And from within bubble we are
passing pointers to the elements of the array that we want to
compare to our comparison function. And, of course we are
dereferencing these pointer in our compare() function in order to
make the actual comparison. Our next step will be to convert the
pointers in bubble() to pointers to type void so that that
function will become more type insensitive. This is shown in
bubble_4.

/*—————— bubble_4.c —————————-*/
#include <stdio.h>

int arr[10] = { 3,6,1,2,3,8,4,1,7,2};

void bubble(int *p, int N);
int compare(void *m, void *n);

int main(void)
{
int i;
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
bubble(arr,10);
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
return 0;
}

void bubble(int *p, int N)
{
int i, j, t;
for(i = N-1; i >= 0; i–)
for(j = 1; j <= i; j++)
if (compare((void *)&p[j-1], (void *)&p[j]))
{
t = p[j-1];
p[j-1] = p[j];
p[j] = t;
}
}

int compare(void *m, void *n)
{
int *m1, *n1;
m1 = (int *)m;
n1 = (int *)n;
return (*m1 > *n1);
}
/*—————— end of bubble_4.c ———————*/

Note that, in doing this, in compare() we had to introduce the
casting of the void pointer types passed to the actual type being
sorted. But, as we’ll see later that’s okay. And since what is
being passed to bubble() is still a pointer to an array of
integers, we had to cast these pointers to void pointers when we
passed them as parameters in our call to compare().

We now address the problem of what we pass to bubble(). We want
to make the first parameter of that function a void pointer also.
But, that means that within bubble() we need to do something
about the variable t, which is currently an integer. Also, where
we use t = p[j-1]; the type of p[j-1] needs to be known in order
to know how many bytes to copy to the variable t (or whatever we
replace t with).

Currently, in bubble_4.c, knowledge within buffer() as to the
type of the data being sorted (and hence the size of each
individual element) is obtained from the fact that the first
parameter is a pointer to type integer. If we are going to be
able to use bubble() to sort any type of data, we need to make
that pointer a pointer to type void. But, in doing so we are
going to lose information concerning the size of individual
elements within the array. So, in bubble_5.c we will add a
separate parameter to handle this size information.

These changes, from bubble4.c to bubble5.c are, perhaps, a bit
more extensive than those we have made in the past. So, compare
the two modules carefully for differences.

/*———————- bubble5.c —————————*/
#include <stdio.h>
#include <string.h>

long arr[10] = { 3,6,1,2,3,8,4,1,7,2};

void bubble(void *p, size_t width, int N);
int compare(void *m, void *n);

int main(void)
{
int i;
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
bubble(arr, sizeof(long), 10);
putchar(’\n’);
for(i = 0; i < 10; i++)
{
printf(”%d “, arr[i]);
}
return 0;
}

void bubble(void *p, size_t width, int N)
{
int i, j;
unsigned char buf[4];
unsigned char *bp = p;
for(i = N-1; i >= 0; i–)
for(j = 1; j <= i; j++)
if (compare((void *)(bp + width*(j-1)), (void *)(bp + j*width))) /* 1 */
{
/* t = p[j-1]; */
memcpy(buf, bp + width*(j-1), width);
/* p[j-1] = p[j]; */
memcpy(bp + width*(j-1), bp + j*width , width);
/* p[j] = t; */
memcpy(bp + j*width, buf, width);
}

}

int compare(void *m, void *n)
{
long *m1, *n1;
m1 = (long *)m;
n1 = (long *)n;
return (*m1 > *n1);
}
/*——————— end of bubble5.c ———————*/

Note that I have changed the data type of the array from int to
long to illustrate the changes needed in the compare() function.
Within bubble I’ve done away with the variable t (which we would
have had to change from type int to type long). I have added a
buffer of size 4 unsigned characters, which is the size needed to
hold a long (this will change again in future modifications to
this code). The unsigned character pointer *bp is used to point
to the base of the array to be sorted, i.e. to the first element
of that array.

We also had to modify what we passed to compare(), and how we do
the swapping of elements that the comparison indicates need
swapping. Use of memcpy() and pointer notation instead of array
notation work towards this reduction in type sensitivity.

Again, making a careful comparison of bubble5.c with bubble4.c
can result in improved understanding of what is happening and
why.

We move now to bubble6.c where we use the same function bubble()
that we used in bubble5.c to sort strings instead of long
integers. Of course we have to change the comparison function
since the means by which strings are compared is different from
that by which long integers are compared. And,in bubble6.c we
have deleted the lines within bubble() that were commented out in
bubble5.c.

/*——————— bubble6.c ———————*/
#include <stdio.h>
#include <string.h>

#define MAX_BUF 256

long arr[10] = { 3,6,1,2,3,8,4,1,7,2};

char arr2[5][20] = { “Mickey Mouse”,
“Donald Duck”,
“Minnie Mouse”,
“Goofy”,
“Ted Jensen” };

void bubble(void *p, int width, int N);
int compare(void *m, void *n);

int main(void)
{
int i;
putchar(’\n’);
for(i = 0; i < 5; i++)
{
printf(”%s\n”, arr2[i]);
}
bubble(arr2, 20, 5);
putchar(’\n\n’);
for(i = 0; i < 5; i++)
{
printf(”%s\n”, arr2[i]);
}
return 0;
}

void bubble(void *p, int width, int N)
{
int i, j, k;
unsigned char buf[MAX_BUF];
unsigned char *bp = p;
for(i = N-1; i >= 0; i–)
for(j = 1; j <= i; j++)
{
k = compare((void *)(bp + width*(j-1)), (void *)(bp + j*width));
if (k > 0)
{
memcpy(buf, bp + width*(j-1), width);
memcpy(bp + width*(j-1), bp + j*width , width);
memcpy(bp + j*width, buf, width);
}
}
}

int compare(void *m, void *n)
{
char *m1 = m;
char *n1 = n;
return (strcmp(m1,n1));
}
/*——————- end of bubble6.c ———————*/

But, the fact that bubble() was unchanged from that used in
bubble5.c indicates that that function is capable of sorting a
wide variety of data types. What is left to do is to pass to
bubble() the name of the comparison function we want to use so
that it can be truly universal. Just as the name of an array is
the address of the first element of the array in the data
segment, the name of a function decays into the address of that
function in the code segment. Thus we need to use a pointer to a
function. In this case the comparison function.

Pointers to functions must match the functions pointed to in the
number and types of the parameters and the type of the return
value. In our case, we declare our function pointer as:

int (*fptr)(const void *p1, const void *p2);

Note that were we to write:

int *fptr(const void *p1, const void *p2);

we would have a function prototype for a function which returned
a pointer to type int. That is because in C the parenthesis ()
operator have a higher precedence than the pointer * operator.
By putting the parenthesis around the string (*fptr) we indicate
that we are declaring a function pointer.

We now modify our declaration of bubble() by adding, as its 4th
parameter, a function pointer of the proper type. It’s function
prototype becomes:

void bubble(void *p, int width, int N,
int(*fptr)(const void *, const void *));

When we call the bubble(), we insert the name of the comparison
function that we want to use. bubble7.c illustrate how this
approach permits the use of the same bubble() function for
sorting different types of data.

/*——————- bubble7.c ——————*/
#include <stdio.h>
#include <string.h>

#define MAX_BUF 256

long arr[10] = { 3,6,1,2,3,8,4,1,7,2};

char arr2[5][20] = { “Mickey Mouse”,
“Donald Duck”,
“Minnie Mouse”,
“Goofy”,
“Ted Jensen” };

void bubble(void *p, int width, int N,
int(*fptr)(const void *, const void *));
int compare_string(const void *m, const void *n);
int compare_long(const void *m, const void *n);
int main(void)
{
int i;
puts(”\nBefore Sorting:\n”);
for(i = 0; i < 10; i++) /* show the long ints */
{
printf(”%ld “,arr[i]);
}
puts(”\n”);
for(i = 0; i < 5; i++) /* show the strings */
{
printf(”%s\n”, arr2[i]);
}
bubble(arr, 4, 10, compare_long); /* sort the longs */
bubble(arr2, 20, 5, compare_string); /* sort the strings */
puts(”\n\nAfter Sorting:\n”);
for(i = 0; i < 10; i++) /* show the sorted longs */
{
printf(”%d “,arr[i]);
}
puts(”\n”);
for(i = 0; i < 5; i++) /* show the sorted strings */
{
printf(”%s\n”, arr2[i]);
}
return 0;
}

void bubble(void *p, int width, int N,
int(*fptr)(const void *, const void *))
{
int i, j, k;
unsigned char buf[MAX_BUF];
unsigned char *bp = p;
for(i = N-1; i >= 0; i–)
for(j = 1; j <= i; j++)
{
k = fptr((void *)(bp + width*(j-1)), (void *)(bp + j*width));
if (k > 0)
{
memcpy(buf, bp + width*(j-1), width);
memcpy(bp + width*(j-1), bp + j*width , width);
memcpy(bp + j*width, buf, width);
}
}
}

int compare_string(const void *m, const void *n)
{
char *m1 = (char *)m;
char *n1 = (char *)n;
return (strcmp(m1,n1));
}

int compare_long(const void *m, const void *n)
{
long *m1, *n1;
m1 = (long *)m;
n1 = (long *)n;
return (*m1 > *n1);
}
/*—————– end of bubble7.c —————–*/

Pointers and Dynamic Allocation of Memory

Posted on February 21st, 2008 in C by admin

Pointers and Dynamic Allocation of Memory There are times when it is convenient to allocate memory at
run time using malloc(), calloc(), or other allocation functions.
Using this approach permits postponing the decision on the size
of the memory block need to store an array, for example, until
run time. Or it permits using a section of memory for the
storage of an array of integers at one point in time, and then
when that memory is no longer needed it can be freed up for other
uses, such as the storage of an array of structures.

When memory is allocated, the allocating function (such as
malloc(), calloc(), etc.) returns a pointer. The type of this
pointer depends on whether you are using an older K&R compiler or
the newer ANSI type compiler. With the older compiler the type
of the returned pointer is char, with the ANSI compiler it is
void.

If you are using an older compiler, and you want to allocate
memory for an array of integers you will have to cast the char
pointer returned to an integer pointer. For example, to allocate
space for 10 integers we might write:

int *iptr;
iptr = (int *)malloc(10 * sizeof(int));
if(iptr == NULL)
{ .. ERROR ROUTINE GOES HERE .. }

If you are using an ANSI compliant compiler, malloc() returns
a void pointer and since a void pointer can be assigned to a
pointer variable of any object type, the (int *) cast shown above
is not needed. The array dimension can be determined at run time
and is not needed at compile time. That is, the “10″ above could
be a variable read in from a data file or keyboard, or calculated
based on some need, at run time.

Because of the equivalence between array and pointer
notation, once iptr has been assigned as above, one can use the
array notation. For example, one could write:

int k;
for(k = 0; k < 10; k++
iptr[k] = 2;

to set the values of all elements to 2.

Even with a reasonably good understanding of pointers and
arrays, one place the newcomer to C is likely to stumble at first
is in the dynamic allocation of multi-dimensional arrays. In
general, we would like to be able to access elements of such
arrays using array notation, not pointer notation, wherever
possible. Depending on the application we may or may not know
both dimensions at compile time. This leads to a variety of ways
to go about our task.

As we have seen, when dynamically allocating a one
dimensional array the dimension can be determined at run time.
Now, when using dynamic allocation of higher order arrays, we
never need to know the first dimension at compile time. Whether
we need to know the higher dimensions depends on how we go about
writing the code. Here I will discuss various methods of
dynamically allocating room for 2 dimensional arrays of integers.

First we will consider cases where the 2nd dimension is known
at compile time.

METHOD 1:

One way of dealing with the problem is through the use of the
“typedef” keyword. To allocate a 2 dimensional array of integers
recall that the following two notations result in the same object
code being generated:

multi[row][col] = 1; *(*(multi + row) + col) = 1;

It is also true that the following two notations generate the
same code:

multi[row] *(multi + row)

Since the one on the right must evaluate to a pointer, the
array notation on the left must also evaluate to a pointer. In
fact multi[0] will return a pointer to the first integer in the
first row, multi[1] a pointer to the first integer of the second
row, etc. Actually, multi[n] evaluates to a pointer to that
array of integers which makes up the n-th row of our 2
dimensional array. That is, multi can be thought of as an array
of arrays and multi[n] as a pointer to the n-th array of this
array of arrays. Here the word “pointer” is being used
to represent an address value. While such usage is common in the
literature, when reading such statements one must be careful to
distinguish between the constant address of an array and a
variable pointer which is a data object in itself.

Consider now:
———————————————–
#include <stdio.h>
#define COLS 5

typedef int RowArray[COLS];
RowArray *rptr;

int main(void)
{
int nrows = 10;
int row, col;
rptr = malloc(nrows * COLS * sizeof(int))
for(row = 0; row < nrows; row++)
for(col = 0; col < COLS; col++)
{
rptr[row][col] = 17;
}
}
————————————————-
Here I have assumed an ANSI compiler so a cast on the void
pointer returned by malloc() is not required. If you are using
an older K&R compiler you will have to cast using:

rptr = (RowArray *)malloc(…. etc.

Using this approach, “rptr” has all the characteristics of an
array name and array notation may be used throughout the rest of
the program. That also means that if you intend to write a
function to modify the array contents, you must use COLS as a
part of the formal parameter in that function, just as we did
when discussing the passing of two dimensional arrays to a
function.

METHOD 2:

In the METHOD 1 above, rptr turned out to be a pointer to
type “one dimensional array of COLS integers”. It turns out that
there is syntax which can be used for this type without the need
of typedef. If we write:

int (char *xptr)[COLS];

the variable xptr will have all the same characteristics as the
variable rptr in METHOD 1 above, and we need not use the
“typedef” keyword. Here xptr is a pointer to an array of
integers and the size of that array is given by the #defined
COLS. The parenthesis placement makes the pointer notation
predominate, even though the array notation has higher
precedence. i.e. had we written

int char *xptr[COLS];

we would have defined xptr as an array of pointers holding the
number of pointers equal to that #defined by COLS. Which is not
the same thing at all. However, arrays of pointers have their
use in the dynamic allocation of two dimensional arrays, as will
be seen in the next 2 methods.

Pointers to Arrays

Posted on February 21st, 2008 in C by admin

Pointers to Arrays Pointers, of course, can be “pointed at” any type of data
object, including arrays. While that was evident when we
discussed program 3.1, it is important to expand on how we do
this when it comes to multi-dimensional arrays.

To review, in Chapter 2 we stated that given an array of
integers we could point an integer pointer at that array using:

int *ptr;

ptr = &my_array[0]; /* point our pointer at the first
integer in our array */

As we stated there, the type of the pointer variable must match
the type of the first element of the array.

In addition, we can use a pointer as a formal parameter of a
function which is designed to manipulate an array. e.g.

Given:

int array[3] = {’1′, ‘5′, ‘7′};

void a_func(int *p);

we can pass the address of the array to the function by making
the call

a_func(array);

This kind of code promotes the mis-conception that pointers and
arrays are the same thing. Of course, if you have followed this
text carefully up to this point you know the difference between a
pointer and an array. The function would be better written (in
terms of clarity) as a_func(int p[]); Note that here
we need not include the dimension since what we are passing is
the address of the array, not the array itself.

We now turn to the problem of the 2 dimensional array. As
stated in the last chapter, C interprets a 2 dimensional array as
an array of one dimensional arrays. That being the case, the
first element of a 2 dimensional array of integers is a one
dimensional array of integers. And a pointer to a two
dimensional array of integers must be a pointer to that data
type. One way of accomplishing this is through the use of the
keyword “typedef”. typedef assigns a new name to a specified
data type. For example:

typedef unsigned char byte;

provides the name “byte” to mean type “unsigned char”. Hence

byte b[10]; would be an array of unsigned characters.

Note that in the typedef declaration, the word “byte” has
replaced that which would normally be the name of our unsigned
char. That is, the rule for using typedef is that the new name
for the data type is the name used in the definition of the data
type. Thus in:

typedef int Array[10];

Array becomes a data type for an array of 10 integers. i.e.

Array my_arr;

declares my_arr as an array of 10 integers and

Array arr2d[5];

makes arr2d an array of 5 arrays of 10 integers each.

Also note that Array *p1d; makes p1d a pointer to an
array of 10 integers. Because *p1d points to the same type as
arr2, assigning the address of the two dimensional array arr2d to
p1d, the pointer to a one dimensional array of 10 integers is
acceptable. i.e. p1d = &arr2d[0]; or p1d = arr2d;
are both correct.

Since the data type we use for our pointer is an array of 10
integers we would expect that incrementing p1d by 1 would change
its value by 10*sizeof(int), which it does. That is sizeof(*p1d)
is 20. You can prove this to yourself by writing and running a
simple short program.

Now, while using typedef makes things clearer for the reader
and easier on the programmer, it is not really necessary. What
we need is a way of declaring a pointer like p1d without the need
of the typedef keyword. It turns out that this can be done and
that int (*p1d)[10]; is the proper declaration, i.e. p1d here
is a pointer to an array of 10 integers just as it was under the
declaration using the Array type. Note that this is different
than int *p1d[10]; which would make p1d the name of an
array of 10 pointers to type int.

More on Multi-Dimensional Arrays

Posted on February 21st, 2008 in C by admin

More on Multi-Dimensional Arrays In the previous chapter we noted that given

#define ROWS 5
#define COLS 10

int multi[ROWS][COLS];

we can access individual elements of the array “multi” using
either:

multi[row][col] or *(*(multi + row) + col)

To understand more fully what is going on, let us replace

*(multi + row) with X as in:

*(X + col)

Now, from this we see that X is like a pointer since the
expression is de-referenced and we know that col is an integer.
Here the arithmetic being used is of a special kind called
“pointer arithmetic” is being used. That means that, since we
are talking about an integer array, the address pointed to by
(i.e. value of) X + col + 1 must be greater than the address
X + col by and amount equal to sizeof(int).

Since we know the memory layout for 2 dimensional arrays, we
can determine that in the expression multi + row as used
above, multi + row + 1 must increase by value an amount
equal to that needed to “point to” the next row, which in this
case would be an amount equal to COLS * sizeof(int).

That says that if the expression *(*(multi + row) + col)
is to be evaluated correctly at run time, the compiler must
generate code which takes into consideration the value of COLS,
i.e. the 2nd dimension. Because of the equivalence of the two
forms of expression, this is true whether we are using the
pointer expression as here or the array expression
multi[row][col].

Thus, to evaluate either expression, a total of 5 values must be
known:

1) The address of the first element of the array, which is
returned by the expression “multi”, i.e. the name of the
array.

2) The size of the type of the elements of the array, in
this case sizeof(int).

3) The 2nd dimension of the array

4) The specific index value for the first dimension, “row”
in this case.

5) The specific index value for the second dimension, “col”
in this case.

Given all of that, consider the problem of designing a
function to manipulate the element values of a previously
declared array. For example, one which would set all the elements
of the array “multi” to the value 1.

void set_value(int m_array[][COLS])
{
int row, col;
for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
{
m_array[row][col] = 1;
}
}
}

And to call this function we would then use:

set_value(multi);

Now, within the function we have used the values #defined by
ROWS and COLS which set the limits on the for loops. But, these
#defines are just constants as far as the compiler is concerned,
i.e. there is nothing to connect them to the array size within
the function. row and col are local variables, of course. The
formal parameter definition informs the compiler that we are
talking about an integer array. We really don’t need the first
dimension and, as will be seen later, there are occasions where
we would prefer not to define it within the parameter definition
so, out of habit or consistency, I have not used it here. But,
the second dimension _must_ be used as has been shown in the
expression for the parameter. The reason is that it is needed in
the evaluation of m_array[row][col] as has been described.
The reason is that while the parameter defines the data type (int
in this case) and the automatic variables for row and column are
defined in the for loops, only one value can be passed using a
single parameter. In this case, that is the value of “multi” as
noted in the call statement, i.e. the address of the first
element, often referred to as a pointer to the array. Thus, the
only way we have of informing the compiler of the 2nd dimension
is by explicitly including it in the parameter definition.

In fact, in general all dimensions of higher order than one
are needed when dealing with multi-dimensional arrays. That is
if we are talking about 3 dimensional arrays, the 2nd _and_ 3rd
dimension must be specified in the parameter definition.

Ted Jensen