Subject: Re: slightly off topic: [*]
To: Perry E. Metzger <>
From: Ian Lance Taylor <>
List: tech-toolchain
Date: 09/23/2005 13:25:14
"Perry E. Metzger" <> writes:

> > I can tell you what it means.  It can be used in a function prototype
> > to mean that the parameter is a variable length array but that the
> > prototype does not say what the actual length is.  For example,
> >     int foo (int i, int a[*]);
> > is a prototype for a function which returns foo and takes two
> > parameters, one of type int and the other being a variable length
> > array.  You can't use [*] in a function definition.  In the
> > definition, you might say
> >     int foo (int i, int a[i]) { }
> > to mean that the variable length array a has i elements.
> Does this have any semantic distinction from the prototype saying
> int foo(int i, int a[]);
> at all? Compiled code has no way of knowing what the main function
> might have inside the [] anyway, so it can't behave differently.

After further reflection, I the issue really is multi-dimensional
arrays.  Let's say you define a function like this:
    static int bar1 (int) { ... }
    static int bar2 (int) { ... }
    int foo (int i, int a[bar1 (i)][bar2 (i)]) { ... }
Here the function accepts a variable length array, but the dimensions
are specifed using a static function.  This function can be called
passing a constant length array, like this:
    int a[5][10];
    foo (1, a);
(of course 5 and 10 must match the return value of bar1 (1) and bar2
(1), respectively, or the resulting program is undefined).

Now, how do we declare foo in a header file?  We can't write this:
    extern int foo (int i, int a[bar1 (i)][bar2 (i)]);
because, for whatever reason, we have decided to make bar1 and bar2
static functions which are presumably not declared in the header
file.  We also can't write this:
    extern int foo (int i, int a[][]);
because that is, effectively, declaring a pointer to an array of
unknown size and therefore incomplete type.

So we have a problem.  We want to say "foo takes a parameter which is
a pointer to a multi-dimensional array, but we can not specify the
inner dimension.  The caller must pass an array of compatible size,
but we can't describe that size."  This leaves us with the choice of
rejecting calls to such a function, because we can not verify that the
array has an appropriate size, or accepting them and trusting the
caller.  The tradition in C is to trust that code gets it right.
Therefore, the committee needed to invent a way to say
"multi-dimensional array of indeterminate size."  They could have
reasonably extended the meaning of int a[][], but, I hypothesize, that
they felt that that would be too error prone--people do often mistakes
using array notation in function prototypes.  Therefore, I
hypothesize, they decided to introduce a new syntax (actually I think
Fortran uses the same syntax) to describe a multi-dimensional array of
unknown size:
    extern int foo (int i, int a[*][*]);