This question is quite similar to "Are conformant array parameters VLAs?", with the only difference being that here I'm using the static keyword within the [ and ] of the array declaration (introduced in C99), meaning that the declared array should have space for at least len elements. Here's an example:
#include <stddef.h>
#include <stdio.h>
void print_array(size_t len, const int arr[static len]) {
for (size_t i = 0; i < len; i++) {
printf("%d\n", arr[i]);
}
}
int main(void) {
const int arr[] = {1, 2, 3, 4};
print_array(sizeof arr / sizeof *arr, arr);
return 0;
}
This code when compiled with the -Wvla flag on both GCC and Clang gives the following warning:
main.c:4:1: warning: ISO C90 forbids variable length array ‘arr’ [-Wvla]
4 | void print_array(size_t len, const int arr[static len]) {
| ^~~~
What would be the point in arr being a VLA even when specifying static? Wouldn't this make static useless for cases like this?
I tried looking for an answer in the C standard, but I was unable to find one.
Thanks!
C 2018 (and C 2011) 6.7.6.2 4 says, of array declarators:
In the declaration
const int arr[static len],lenis not an integer constant expression, so the array type is a variable length array type. So GCC is correct.6.7.6.3 7 says:
An example of how a compiler could use this is that, if the body of the function contains an evaluated expression
arr[i], wherearrhas been declared withconst int array[static len], the compiler may presumei<len, and it could make optimizations based on that.No, although it would not surprise me if a compiler does not take advantage of this information.