Comment by WalterBright
14 hours ago
C: int foo(int a[]) { return a[5]; }
int main() {
int a[3];
return foo(a);
}
> gcc test.c
> ./a.out
Oops.
D: int foo(int[] a) { return a[5]; }
int main() {
int[3] a;
return foo(a);
}
> ./cc array.d
> ./array
core.exception.ArrayIndexError@array.d(1): index [5] is out of bounds for array of length 3
Ah, Nirvana!
How to fix it for C:
You need to take the address of the array instead of letting it decay and then size is encoded in the type:
Or for run-time length:
https://godbolt.org/z/dxx7TsKbK\*