<pertinent references reinserted, to quote from
others on this group,
"Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.">
>> Let say
>> char name[9]="123456789"
>> If entered the name in a loop;
>>
>
Ian said:
> In this assignment the compiler copies from the
> constant null terminated string, which has elements
> "123456789\0" to the array 'name', which has space
> for 9 elements, so it stops at '9'. What you get
> is not a null terminated string, but a nine char
> long array holding the characters 1 to 9.
What you mean that is always a must to leave en element space in the
end of an array string to copy the null character by the compiler ?
If I did not what are the consequneces ?
It is not a must, but it does make sense. The consequence
in this case is that the null terminating character is not
copied. As a result what you have is an array of characters,
not a C-style null terminated array. This means most (all?)
of the standard functions for strings (such as strlen) will
not work with it. The least error prone solution is to
provide an array of unknown size to be initialized.
>> char name[] = "123456789"
What I'm curious about, if anyone reading knows, is what happens
when the array is not large enough to hold a character in the
initializer other than '\0'?
6.7.8 of C9X FCD:
[#2] No initializer shall attempt to provide a value for an
object not contained within the entity being initialized.
[#14] An array of character type may be initialized by a
character string literal, optionally enclosed in braces.
Successive characters of the character string literal
(including the terminating null character if there is room
or if the array is of unknown size) initialize the elements
of the array.
14 seems to make special provision for the case of the null
character alone, 2 seems to imply the general case:
char too_short[1] = "too_long";
Is undefined: am I reading that right?