News for hackers. Stuff that matters.
Re:Checked C by int_19h on Friday December 23, 2022 @06:09PM (#34111678)
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.

There are no comments on this comment yet.