C++ arrays and size
If I have for example the following array declaration: int a[5];
, this means that I have an array "a" that holds 5 integer variables.
Wouldn't it be in memory something like this (See the \0
)?
|0|1|2|3|4|\0|
So, in this case, do I still say that a[]
is of size 5
or should I say it is of size 6
?
And, if I copy it to the arra开发者_JAVA技巧y: int b[4] what will happen in this case? What will happen to the integer variable in location 4
of a[]
, will it overwrite \0
?
Thanks.
Arrays are not, in general, automatically zero-terminated. C strings are zero-terminated, but this is not a C string.
There are only |0|1|2|3|4| in the memory, not the sixth one. So the size is 5.
Great explanation of arrays for beginners.
精彩评论