Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: What is the meaning of array parameters? Date: Tue, 25 Nov 2025 13:25:18 -0800 Organization: None to speak of Lines: 74 Message-ID: <87fra1kdoh.fsf@example.invalid> References: <10g4au7$jc8o$1@solani.org> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Tue, 25 Nov 2025 21:25:23 +0000 (UTC) Injection-Info: dont-email.me; posting-host="fa7a31f1160d6b9aea861ca62102f1fd"; logging-data="4008114"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/XV+km2BNfk8lLWDaU/u2G" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:qYDvFdez+YtIB/CMYuz3UkPpZZQ= sha1:3JGxSKlHAHDyJbkVYDTVrVuuZNA= Xref: csiph.com comp.lang.c:395482 Philipp Klaus Krause 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, . 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 */