Do you know that, for statically defined arrays, you can get the size like this:
int foo[10];
int ten = sizeof(foo)/sizeof(foo[0]);
Of course, that doesn’t work if you do that with a function argument because you can’t pass arrays as arguments, only pointers, so a function can’t know whether a passed value is a statically defined array or dynamically allocated.
int f(int * foo) {
return sizeof(foo)/sizeof(foo[0]); //
}
int notTen = f(foo);
That even is the case when you do things like these (
https://stackoverflow.com/a/2375038):
int f(int foo[10]) {
return sizeof(foo)/sizeof(foo[0]);
}
int notTen = f(foo);
or
int f(int foo[static 10]) {
return sizeof(foo)/sizeof(foo[0]);
}
int notTen = f(foo);