Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #395482
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What is the meaning of array parameters? |
| Date | 2025-11-25 13:25 -0800 |
| Organization | None to speak of |
| Message-ID | <87fra1kdoh.fsf@example.invalid> (permalink) |
| References | <10g4au7$jc8o$1@solani.org> |
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 */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
What is the meaning of array parameters? Philipp Klaus Krause <pkk@spth.de> - 2025-11-25 14:28 +0100
Re: What is the meaning of array parameters? Michael S <already5chosen@yahoo.com> - 2025-11-25 16:04 +0200
Re: What is the meaning of array parameters? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-11-25 13:25 -0800
Re: What is the meaning of array parameters? Philipp Klaus Krause <pkk@spth.de> - 2025-11-27 10:34 +0100
Re: What is the meaning of array parameters? antispam@fricas.org (Waldek Hebisch) - 2025-11-29 22:28 +0000
Re: What is the meaning of array parameters? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-11-29 22:01 -0500
Re: What is the meaning of array parameters? Andrey Tarasevich <noone@noone.net> - 2025-12-05 06:01 -0800
Re: What is the meaning of array parameters? Lynn McGuire <lynnmcguire5@gmail.com> - 2025-12-05 15:36 -0600
csiph-web