Philipp Klaus Krause <
pkk@spth.de> writes:
Since C99 we have four types of array parameters:
[]
[assignment-expression]
[static assignment-expression]
[*]
But what is their meaning? They're all compatible, they all decay to
pointers anyway. Only to [static assignment-expression] does the
standard give a little bit of extra formal meaning, by making it UB
when a too-short array is passed.
They exist, they are different types, but the standard does not give
them meaning (with the exception noted above). So people using them
must have a motivation beyond what is explicitly stated in the
standard, and thus an idea of what the meaning of these would or
should be.
The [] form is of course the simplest, where
void foo(int param[]);
is equivalent to
void foo(int *param);
You can optionally include bounds information, which the compiler
quietly ignores (after checking for legality). This is presumably
intended as documentation for the reader. For example, the time()
function originally took a pointer to the initial element of an array of
two ints (since int was only 16 bits and there was no long type). The declaration was something like:
time(tvec)
int tvec[2];
which in more modern C would be:
int time(int tvec[2]);
If you provided something other than a pointer to the 0th element of an
array of two ints, that was just too bad.
Quietly ignoring bounds information in array-like parameters is not one
of C's best features.
See also section 6 of the comp.lang.c FAQ, <
https://www.c-faq.com/>.
The "static" form was added in C99. This is covered in the C99
Rationale :
https://www.open-std.org/jtc1/sc22/WG14/www/C99RationaleV5.10.pdf
The static keyword provides useful information about the intent
of function parameters.
It would be a significant advantage on some systems for the
translator to initiate, at the beginning of the function,
prefetches or loads of the arrays that will be referenced
through the parameters. There is no way in C89 for the user to
provide information to the translator about how many elements
are guaranteed to be available.
The [*] syntax defines a VLA parameter (a pointer to the initial element
of a variable-length array). It can be used only in a prototype that's
not part of a function definition. An example from cppreference.com :
void foo(size_t x, int a[*]);
void foo(size_t x, int a[x])
{
printf("%zu\n", sizeof a); // same as sizeof(int*)
}
https://en.cppreference.com/w/c/language/array.html
It's still up to the caller to ensure that the value of x is correct.
--
Keith Thompson (The_Other_Keith)
Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
--- PyGate Linux v1.5.1
* Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)