If I have a C string initialization like this:
char a[5]={'a','b','c'};
Is this valid? What's the value of a[3] and a[4]? Is the null character automatically attached to the end of a?
Thanks!
If I have a C string initialization like this:
char a[5]={'a','b','c'};
Is this valid? What's the value of a[3] and a[4]? Is the null character automatically attached to the end of a?
Thanks!
They would be initialized to a value of 0. Null is automatically added when you leave some elements uninitialized.
It depends on what C
std you are following:
If you follow C90
STRICTLY then it forbids you to do it, Thanks BLUEPIXY for the enlightenment
Then, there are certain extensions to these standards which enable you to compile this code without any error, in which case, this is fine.
So, in case of the later, C
language takes care by initializing rest to zero .
char a[5] = {}
initializes all indexes to zero.
If called within a function a[3] and a[4] will be uninitiallised. That is to say, they will be whatever happens to be sitting in memory at the time.
If declared outside a function, a[3] and a[4] will be set to 0.
Depending on the implementation I believe that the last elements of an array will be either completely random or just equivalents of 0's or even something else, but besides of that array will get initialized just fine.