[CC += Kees, Qing] Hi Joseph,
On Sun, Aug 04, 2024 at 08:34:24PM GMT, Alejandro Colomar wrote:
> On Sun, Aug 04, 2024 at 08:02:25PM GMT, Martin Uecker wrote:
> D'oh! I screwed it. I wanted to have written this:
>
> $ cat star.c
> void foo(char (*a)[3][*], int (*x)[__lengthof__(*a)]);
I think this answers your question of if we want __lengthof__ to
evaluate its operand if the top-level array is non-VLA but an inner
array is VLA.
We clearly want it to not evaluate, because we want this __lengthof__
to be a constant expression, ...
> void bar(char (*a)[*][3], int (*x)[__lengthof__(*a)]);
> void foo2(char (*a)[3][*], int (*x)[sizeof(**a)]);
> void bar2(char (*a)[*][3], int (*x)[sizeof(**a)]);
>
> int
> main(void)
> {
> int i3[3];
> int i5[5];
> char c35[3][5];
> char c53[5][3];
>
> foo(&c35, &i3);
> foo(&c35, &i5); // I'd expect this to err
... and thus cause a compile-time error here
(-Wincompatible-pointer-types).
I suspect we need to modify array_type_nelts_minus_one() for that; I'm
going to investigate.
Have a lovely day!
Alex
> bar(&c53, &i3); // I'd expect this to warn
> bar(&c53, &i5);
>
> foo2(&c35, &i3); // I'd expect this to warn
> foo2(&c35, &i5);
> bar2(&c53, &i3);
> //bar2(&c53, &i5); // error: -Wincompatible-pointer-types
> }
> $ /opt/local/gnu/gcc/lengthof/bin/gcc -Wall -Wextra star.c -S
> $
--
<https://www.alejandro-colomar.es/>
signature.asc
Description: PGP signature
