In standard C, you have a few options for declaring a function that accepts a pointer to a chunk of data:
void style_1(int * arr);
void style_2(int arr[]);
void style_3(int arr[10]);
void style_4(int size, int arr[size]);
void style_5(int arr[static 10]);
void style_6(int size, int arr[static size]);
The thing is, as near as I can tell, most of the "choice" is syntactic and doesn't contribute much to the program's actual meaning. The first three styles appear to be exactly equivalent, because "A declaration of a parameter as 'array of type' shall be adjusted to 'qualified pointer to type'" (C11 6.7.6.3 p7), and style_4
is also the same for the purposes of the pointer parameter. For both style_3
and style_4
, the array size is thrown away.
style_5
and style_6
are actually different and impose a useful constraint, guaranteeing a non-null pointer with a minimum amount of storage. But why do we need to add a keyword to do this? The declarations of style_3
and style_4
seem to communicate just as much information to the reader. A compiler would surely have no problem with those, if only the language allowed them to be meaningful? As it is, this design choice seems to introduce a hole that can allow extra errors to slip through, by effectively instructing the machine not to check for them by default.
Why was static
added to impose the size constraint on array parameters in C? Why is the size on its own not sufficient?
int arr[static 10]
, then passing an argument that doesn't point to the initial element of an array with at least 10 elements has undefined behavior. The point is to allow the compiler to optimize based on the assumption that the argument does properly point to such an object.