Sized array w/initializer

L

Leo Havmøller

Hej,

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?

Leo Havmøller.
 
S

Seebs

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.

-s
 
P

Peter Nilsson

Leo Havmøller said:
When array size specified conflicts the initializer e.g.:
char s[5] = "0123456789";
A quick test with a random compiler

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.
 
P

Phil Carmody

Seebs said:
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
 
G

Gil Johnson

* 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'.

Gil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top