Sujet : Re: Loops (was Re: do { quit; } else { })
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 11. May 2025, 22:41:22
Autres entêtes
Organisation : None to speak of
Message-ID : <87r00ukepp.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
User-Agent : Gnus/5.13 (Gnus v5.13)
David Brown <
david.brown@hesbynett.no> writes:
[...]
Using "static" inside array parameters is, IME, extremely rare. It
was added in C99, and tells the compiler that whenever "average" is
called, the "values" parameter points to an array of at least 10
doubles. It does not affect the signature of the function or
compatibility with any other declarations, and is AFAIK rarely checked
by compilers.
More precisely, it says that if you pass an argument that points
to the initial element of an array that's shorter than what's
specified in the parameter declaration, the behavior is undefined.
A conforming compiler could ignore it, but both gcc and clang
use this to enable warnings on calls. (clang's warning is a bit
clearer.)
```
$ cat c.c
static void func(int arr[static 4]) {
}
int main(void) {
int arg[3];
func(arg);
}
$ gcc -c c.c
c.c: In function ‘main’:
c.c:6:5: warning: ‘func’ accessing 16 bytes in a region of size 12 [-Wstringop-overflow=]
6 | func(arg);
| ^~~~~~~~~
c.c:6:5: note: referencing argument 1 of type ‘int[4]’
c.c:1:13: note: in a call to function ‘func’
1 | static void func(int arr[static 4]) {
| ^~~~
$ clang -c c.c
c.c:6:5: warning: array argument is too small; contains 3 elements, callee requires at least 4 [-Warray-bounds]
6 | func(arg);
| ^ ~~~
c.c:1:22: note: callee declares array parameter as static here
1 | static void func(int arr[static 4]) {
| ^ ~~~~~~~~~~
1 warning generated.
```
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */