3 ms·
GCC also has an extension to support references to other parameters of the function: #include <stddef.h> void foo(size_t n, int b[static n]); https://
by nikeee 10mo ago
GCC also has an extension to support references to other parameters of the function:
#include <stddef.h>
void foo(size_t n, int b[static n]);
https://godbolt.org/z/c4o7hGaG1 https://godbolt.org/z/c4o7hGaG1
It is not limited to compile-time constants. Doesn't work in clang, sadly.
- fuhsnn 10mo agoClang is working on a different version with annotations https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3656.pdf https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3656.pdf
- eqvinox 10mo agoIt also only works with that order, not if the size is after the array :(
- kragen 10mo agoNo, you can predeclare the size; this compiles with no warnings: #include <string.h> #include <unistd.h> void foo(size_t n; const char s[static n], size_t n) { write(1, s, n); } int main(int argc, char **argv) { foo("hello, ", 7); if (argc > 1) foo(argv[1], strlen(argv[1])); foo("\n", 1); return 0; } However, it still compiles with no warnings if you change 7 to 10! Clang does not support this syntax.