4 ms·
> The sizes of arrays are not known in any useful way to C. When I declare a variable of type int[5] in a function, I don’t get a value of type int[5]; I get an
by dahfizz 4y ago
> The sizes of arrays are not known in any useful way to C. When I declare a variable of type int[5] in a function, I don’t get a value of type int[5]; I get an int* value which has 5 ints allocated at it. Since this is just a pointer, the programmer, not the language, has to manage copying the data behind it and keeping it valid.
This is not quite correct. Assuming arrays == pointers is usually true enough, but it isn't actually true. This[1] SO thread has some useful information in it, but the TLDR is that actual arrays are treated differently than pointers. You do get an object of type "int array" rather than "int pointer" when you do int[5].
The compiler does know the size of an array allocated like T a[n]. It does not, however, do any bounds checking for you.
[1] https://stackoverflow.com/questions/4607128/in-c-are-arrays-pointers-or-used-as-pointers https://stackoverflow.com/questions/4607128/in-c-are-arrays-...