|
|
|
|
|
by wahern
1 day ago
|
|
> Can you do that with a dynamic array? Yes. #include <stdio.h>
#if __has_include(<stdcountof.h>)
#include <stdcountof.h>
#else
#define countof(a) (sizeof (a) / sizeof *(a))
#endif
void
foo(int n, int a[static 1][n])
{
printf("sizeof *a: %zu\n", sizeof *a);
printf("countof *a: %zu\n", countof(*a));
}
int
main(int argc, char *argv[])
{
int array[argc];
foo(countof(array), &array);
return 0;
}
$ ./foo
sizeof *a: 4
countof *a: 1
$ ./foo 2 3
sizeof *a: 12
countof *a: 3
Tested using Apple clang 21.0.0 and gcc 15.2.0.The syntax for using passed VM arrays is stilted; you're operating on a pointer to an array, which can get confusing and is error prone. Because of the semantics for array passing and need for backward compatibility it's too easy to get it wrong without the compiler catching mistakes and complaining. Though, the C2y _Countof operator is required to error when used on a non-array, so using _Countof(a) instead of _Countof(*a) will fail. (GCC and clang also have warning diagnostics that work for the fallback countof macro.) And there's no way (or no easy way?) to ask compilers to inject automatic bounds checking when operating on arrays, at least outside non-production debugging modes like ASan. But C is getting there, slowly. |
|