2 ms·
To pass fixed-size arrays in a way that preserves the type and allows checking their bounds, you can use pointers-to-arrays: static void foo(int (*a)[6]) {
by int_19h 4y ago
To pass fixed-size arrays in a way that preserves the type and allows checking their bounds, you can use pointers-to-arrays:
static void foo(int (*a)[6]) {
printf("%d\n", (*a)[8]); // warning: array index 8 is past the end of the array (which contains 6 elements)
}
int main() {
int a[5] = {1, 2, 3, 4, 5};
foo(&a); // warning: incompatible pointer types passing 'int (*)[5]' to parameter of type 'int (*)[6]'
}
This is all vanilla C89, so it works even with very old compilers, so long as they're conformant.