Subject: Re: slightly off topic: [*]
To: None <tech-toolchain@NetBSD.org>
From: Alan Barrett <apb@cequrux.com>
List: tech-toolchain
Date: 09/23/2005 16:23:23
On Fri, 23 Sep 2005, Perry E. Metzger wrote:
> >> Slightly off topic but I figure this is as good a place to ask as any
> >> -- does anyone here understand what the C99 "[*]" syntax is for? So
> >> far I've asked lots of smart people and no one can answer.

It allows you to declare prototypes for functions taking variable length
arrays, while also omitting the names of the parameters.  For example,
if you have a function like

	int f(int rows, int cols, int array[rows][cols])
	{
		/* do stuff */
	}

then you could declare a prototype like this:

	int f(int rows, int cols, int array[rows][cols]);

(including variable names in the prototype).  If you try to leave
out the paramater names, like this:

	int f(int, int, int [rows][cols]);

you get an error because "rows" and "cols" are not in scope at the
point where you try to use them to give the array size.  If you try to
leave them out as well, like this:

	int f(int, int, int [][]);

you get another error because you are not allowed to have more than one
level of "[]" (because of the relationship between "var[]" and
"*var").  The solution is to allow prototypes like this:

	int f(int, int, int [*][*]);

without variable names, and with "[*]" just being a convenient
way of saying something other than "[]".

> 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.

For one-dimensional arrays, it makes no real difference in the caller.
In the function itself, the compiler could be stricter about subscript
range checking if the function is defined with

	int foo(int i, int a[i]);

rather than

	int foo(int i, int a[]);

or

	int foo(int i, int *a);

For multi-dimensional arrays, you get syntax errors with "int a[][]",
and "int **a" is semantically different from a two-dimensional array.

--apb (Alan Barrett)