Sized array w/initializer

When array size specified conflicts the initializer e.g.:
char s[5] = "0123456789";
A quick test with a random compiler at hand shows that it reserves space for
11 chars, but what does the standard say about it? 

Advertisements

On 2010-04-21, Leo Havmøller <> wrote:
> When array size specified conflicts the initializer e.g.:
> char s[5] = "0123456789";
> A quick test with a random compiler at hand shows that it reserves space for
> 11 chars, but what does the standard say about it? 

* If the string is shorter than the specified size of the array,
it is padded with '\0'. 
* If the string is exactly the size of the array, the array is populated
with the contents of the string. 
* If the string (including the terminator) is exactly one character larger
than the array, the bytes from the string are stored in the array, and
the array is not NUL-terminated. 
* If the string is larger than the array, the compiler is supposed to
give you a diagnostic, after which it may do anything it wants. 

Advertisements

In conforming mode? 
> at hand shows that it reserves space for 11 chars, but what
> does the standard say about it? 

5.1.1.3p1 A conforming implementation shall produce at least
one diagnostic message (identified in an implementation-
defined manner) if a preprocessing translation unit or
translation unit contains a violation of any syntax rule or
constraint, ...

6.7.8p2 [Constraint] No initializer shall attempt to provide
a value for an object not contained within the entity being
initialized. 

6.7.8p14 ... 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. 

Seebs <> writes:
> On 2010-04-21, Leo HavmÃ¸ller <> wrote:
>> When array size specified conflicts the initializer e.g.:
>> char s[5] = "0123456789";
>> A quick test with a random compiler at hand shows that it reserves space for
>> 11 chars, but what does the standard say about it? 
>
> * If the string is shorter than the specified size of the array,
> it is padded with '\0'. 

Not 'string', as strings already have a '\0' at the end:

7.1.1 Definitions of terms

[#1] A string is a contiguous sequence of characters
terminated by and including the first null character.  The

You should probably use '[character] string literal' instead. 
Usage of that term includes:

[#5] In translation phase 7, a byte or code of value zero is
appended to each multibyte character sequence that results
from a string literal or literals. 

So a 'string literal' does not yet necessarily have the '\0' at the end. 
(And may contain embedded '\0' characters too, which a string can not.) 

And I can't say I like 'pad' either.  That implies some kind of
resizing to fit.  The NIL's just appended, nothing more. 
> * If the string is exactly the size of the array, the array is populated
> with the contents of the string. 
> * If the string (including the terminator) is exactly one character larger
> than the array, the bytes from the string are stored in the array, and
> the array is not NUL-terminated. 
> * If the string is larger than the array, the compiler is supposed to
> give you a diagnostic, after which it may do anything it wants. 

I think it's best to just drop references to 'string', they confuse
matters. 

In fact, it's hard to just beat the elegance of the original:
[#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. 
+
[#2] No initializer shall attempt to provide a value for an
object not contained within the entity being initialized. 

Phil
--
I find the easiest thing to do is to k/f myself and just troll away
-- David Melville on r.a.s.f1

On Apr 21, 2:34 am, Phil Carmody <>
wrote:
> Seebs <> writes:
> <snip>
>
> > * If thestringis shorter than the specified size of thearray,
> > it is padded with '\0'. 
>
> Not 'string', as strings already have a '\0' at the end:
>
> 7.1.1 Definitions of terms
>
> [#1] A string is a contiguous sequence of characters
> terminated by and including the first null character.  The
>
> You should probably use '[character]stringliteral' instead. 
> Usage of that term includes:
>
> [#5] In translation phase 7, a byte or code of value zero is
> appended to each multibyte character sequence that results
> from a string literal or literals. 
>
> So a 'stringliteral' does not yet necessarily have the '\0' at the end. 
> (And may contain embedded '\0' characters too, which astringcan not.) 
>
> And I can't say I like 'pad' either.  That implies some kind of
> resizing to fit.  The NIL's just appended, nothing more. 

Padding is exactly "some kind of resizing to fit."  Your examples refer
to the translation of the literal, for instance "0123456789", to a
string. 

The phrase "If the string is shorter than the specified size of the
array, it is padded with '\0' " refers to the copying of the string
into the array. 

If a short string, e.g. "012345'\0' " (already terminated with one
'\0') is put in an array of greater than 7 chars, extra characters are
added to the end to bring the string length up to 10.  In this case, C
specifies that those extra characters (the padding) are '\0'. 

Share This Page

Welcome to The Coding Forums! 

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages. 

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free.  You'll be able to ask questions about coding or chat with the community and help others. 
Sign up now!