The following code:
```
--- test.d
import bar;
void main()
{
int len;
foo(&len);
}
--- bar.c
struct nk_utfmask
{ int test; };
void foo(int* i)
{
// #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
// for(*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i))
// expended using `cpp -E` on linux
//
for(*i = 0; *i < (int)(sizeof(nk_utfmask)/sizeof(nk_utfmask)[0]); ++(*i)) {
}
}
```
Error:
bar.c(10): Error: found `[` when expecting `)`
bar.c(10): Error: found `0` when expecting `;` following `for` condition
bar.c(10): Error: expression expected, not `]`
bar.c(10): Error: found `)` when expecting `;` following statement
Apparently it's not valid C11, but that's the preprocessor output, so i'm not sure what i should do, i lack understanding on the syntax itself unfortunately
Comment #1 by ryuukk.dev — 2022-10-28T00:33:04Z
If that's not valid C11, then what would be the proper fix? so i could notify the project owners about a possible PR to make it compatible with C11
> That NK_LEN macro apparently makes use of a weird special syntax to get the length of a C array, see the last 'notes' paragraph in https://en.cppreference.com/w/c/language/sizeof.
So it is valid C11 code? or should this issue be closed?
Comment #5 by bugzilla — 2023-01-19T03:18:53Z
(In reply to kinke from comment #2)
> That NK_LEN macro apparently makes use of a weird special syntax to get the
> length of a C array, see the last 'notes' paragraph in
> https://en.cppreference.com/w/c/language/sizeof.
The cite says:
"Number of elements in any array a including VLA (since C99) may be determined with the expression sizeof a / sizeof a[0]. Note that if a has pointer type (such as after array-to-pointer conversion of function parameter type adjustment), this expression would simply divide the number of bytes in a pointer type by the number of bytes in the pointed type."
Comment #6 by bugzilla — 2023-01-19T03:25:05Z
(In reply to ryuukk_ from comment #0)
> // #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
The usual way this is written:
#define NK_LEN(a) (sizeof(a)/sizeof((a)[0]))
But in order to work, `a` must be a static array. nk_utfmask is a struct. It will also fail because in C, it would have to be `struct nk_utfmask`, not just `nk_utfmask`.
I'm going to mark this as invalid.