3 ms·
Quite surprised to see this not mentioned. C99 allows you to use the "static" keyword in array function parameters like this: void foo(int arr[static 10]);
by bluetomcat 8y ago
Quite surprised to see this not mentioned. C99 allows you to use the "static" keyword in array function parameters like this:
void foo(int arr[static 10]);
It cannot check whether a passed pointer will point to enough space, but the compiler can warn you if you pass a fixed-size array of a smaller size.
- WalterBright 8y agoDynamic arrays are far, far more common than static ones.
- bluetomcat 8y agoI beg to differ. In C especially, static arrays are quite common as struct members and as static objects at file scope, because dynamic allocations are a pain to manage and unnecessary when the maximum expected size is reasonably small. When the size of such arrays is computed at compile-time via macro definitions, that feature is quite handy.
- WalterBright 8y ago> when the maximum expected size I pretty much never use static arrays because if I do I always without fail get a bug report when some user exceeds it.
- jibal 8y ago"In C especially, static arrays are quite common as struct members and as static objects at file scope" Not among good programmers (unless the array is immutable).