Sized array w/initializer

Discussion in 'C Programming' started by Leo Havmøller, Apr 21, 2010.

  1. 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.
    Leo Havmøller, Apr 21, 2010
    #1
    1. Advertising

  2. Leo Havmøller

    Seebs Guest

    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.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Apr 21, 2010
    #2
    1. Advertising

  3. Leo Havmøller <> wrote:
    > 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.

    --
    Peter
    Peter Nilsson, Apr 21, 2010
    #3
  4. Leo Havmøller

    Phil Carmody Guest

    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
    Phil Carmody, Apr 21, 2010
    #4
  5. Leo Havmøller

    Gil Johnson Guest

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

    Gil
    Gil Johnson, Apr 22, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thechaosengine

    Oddly sized sized password textbox

    thechaosengine, Sep 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    571
    David Hearn
    Sep 15, 2005
  2. Allen

    run-time sized array

    Allen, Aug 23, 2003, in forum: C++
    Replies:
    7
    Views:
    506
    J. Campbell
    Aug 23, 2003
  3. Daniel T.

    Dynamic sized array?

    Daniel T., Oct 17, 2006, in forum: C++
    Replies:
    4
    Views:
    314
    Sumit Rajan
    Oct 17, 2006
  4. MartinBroadhurst

    List as a dynamic array of increasing-sized arrays

    MartinBroadhurst, Oct 21, 2010, in forum: C Programming
    Replies:
    13
    Views:
    1,052
    MartinBroadhurst
    Nov 3, 2010
  5. joe
    Replies:
    1
    Views:
    351
    Goran
    Aug 31, 2011
Loading...

Share This Page