I have come across some C code that compiles, but I do not understand why. Specifically, I have a C library that has a lot of code using this format:
void ge
When a parameter is declared as having a single-dimensional array type, C ignores the given size and instead treats the parameter as a pointer to the element type. For nested (multi-dimensional) arrays, such treatment is only applied to the outer array. In C89, inner dimensions had to have fixed sizes, but in C99 the dimensions can be expressions. If parameters which are needed to compute an array's size are not listed until after the array, it will be necessary to use a curious mixture of old and new syntax to declare the function, e.g.
int findNonzero(short dat[*][*], int rows, int cols);
int findNonzero(dat, rows, cols)
int rows,cols;
short dat[static rows][cols];
{
for (int i=0; i
Note that the array sizes are specified as *
in the function prototype, and that the function definition does not specify types in the argument list but instead describes all the parameters' types between the argument list and the opening brace. Note also that while the compiler is likely to ignore the number of rows in the array declaration, but a smart compiler may be able to use it to facilitate optimization. Effectively, the weird "static" syntax invites the compiler to read any parts of the array, up to the given size, as it sees fit, whether or not the values are read by the code. This may be helpful on some platforms where code might benefit from processing multiple items of the array at once.