views:

102

answers:

1

While browsing some source code I came across a function like this:

void someFunction(char someArray[static 100])
{
    // do something cool here
}

With some experimentation it appears other qualifiers may appear there too:

void someFunction(char someArray[const])
{
    // do something cool here
}

It appears that qualifiers are only allowed inside the [ ] when the array is declared as a parameter of a function. What do these do? Why is it different for function parameters?

+5  A: 

The first declaration tells the compiler that someArray is at least 100 Elements long. This can be used for optimizations. For example it also means that someArray is never NULL.

The second declaration simply declars someArray (not someArray's elements!) as const, i.e. you can not write someArray=someOtherArray

It's different for function parameters because it really on makes sense for function parameters. If you declare an array, then you've actually told the compiler how many elements there are. And an array variable with unspecified size (int x[]), has a different meaning than an array parameter (the variable must be initialized and yields a sizeof() of it's raw size)

UPDATE: I'm a bit on a loss to find the clause in C99 which defines this. There's a clause 6.7.5.3-21 which says:

[...]
void f(double a[restrict static 3][5]);
(Note that the last declaration also specifies that the argument corresponding to a in any call to f must be a non-null pointer to the first of at least three arrays of 5 doubles, which the others do not.)

and that's it. Anybody there with C99-fu?

Apparently yes, thank you @Gilles (from the comments):

In N1124 and N1256, the defining clause is 6.7.5.3-7: "If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression."

UPDATE: http://publib.boulder.ibm.com/infocenter/zos/v1r9/index.jsp?topic=/com.ibm.zos.r9.cbclx01/static_array_index.htm

Explicitly recommends "static" to indicate non-null array parameters.

Luther Blissett
On this topic: I wonder if it should be considered preferable to use `int foo(struct bar [static 1]);` instead of `int foo(struct bar *);` as the signature for functions which do not accept NULL pointers. (I know gcc has an alternate nonstandard syntax to flag such functions so that the compiler can give warnings..)
R..
I've just checked gcc and clang and neither assume that someArray is always non null when I ask them to compare against 0. Also I struggle to find the exact clause in C99 which defines it. There's a note in 6.7.5.3-21 which mentions the intended meaning and that's it. I doubt that we can rely on this. Furthermore, all this is not part of the function signature, so there not much that we enforce through it.
Luther Blissett
In N1124 and N1256, the defining clause is 6.7.5.3-7: "If the keyword `static` also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression."
Gilles
@Gilles: Thank you!
Luther Blissett