char array initialization

Discussion in 'C Programming' started by Rox, Aug 12, 2011.

  1. Rox

    Rox Guest

    Hi group,

    For array initialization, I found this in [n1539] 6.7.9

    14.
    An array of character type may be initialized by a character string literal
    or UTF−8 string
    literal, optionally enclosed in braces. Successive bytes of the 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.
    19 ...
    all subobjects that are not initialized explicitly shall be initialized
    implicitly the same as
    objects that have static storage duration.
    and

    So I presume:

    char c[10] = "";

    Will initialize c into 0. Just like memset do.
    It's such a neat way to initial a string that I can't believe it!
    Am I right?

    PS: I verified this on several gcc versions, both gcc and g++, and it just
    WORK

    Rox
     
    Rox, Aug 12, 2011
    #1
    1. Advertising

  2. Rox <> wrote:
    > So I presume:


    > char c[10] = "";


    > Will initialize c into 0. Just like memset do.
    > It's such a neat way to initial a string that I can't believe it!
    > Am I right?


    Yes, it has been that way for a long time. In C89 (3.5.7) you
    have:

    | If there are fewer initializers in a list than there are members of
    | an aggregate, the remainder of the aggregate shall be initialized
    | implicitly the same as objects that have static storage duration.

    If I'm not completely mistakent his means that this trick does
    not only work for char arrays but for all kinds of arrays. So

    int i[ 10 ] = { 1 };

    will initialize the first element of i to 1 and the rest to 0.
    (And if you use 0 as the only initializer you will get an array
    with all 10 elements set to 0.)

    Your example of

    char c[ 10 ] = "";

    is just a special case of this since it's equivalent to

    char c[ 10 ] = { '\0' };

    And, a more complex example directly taken from C89

    | float z[4][3] = {
    | { 1 }, { 2 }, { 3 }, { 4 }
    | };
    |
    | initializes the first column of z as specified and initializes the
    | rest with zeros.

    I would expect this to hold also for structures, so that

    struct { int i; double d; } s = { 1 };

    would have the effect of setting s.i to 1 and s.d to 0.0.
    This results in a warning when using gcc which complains about
    a missing initializer for s.d (but seems to do the expected).
    I guess gcc is a bit overzealous here at the warning level I
    am using (but then a compiler is free to warn about anything
    it doesn't like).
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Aug 12, 2011
    #2
    1. Advertising

  3. Rox

    Paul N Guest

    On Aug 12, 11:59 am, (Jens Thoms Toerring) wrote:
    > Rox <> wrote:
    > > So I presume:
    > > char c[10] = "";
    > > Will initialize c into 0. Just like memset do.
    > > It's such a neat way to initial a string that I can't believe it!
    > > Am I right?

    >
    > If I'm not completely mistakent his means that this trick does
    > not only work for char arrays but for all kinds of arrays. So
    >
    > int i[ 10 ] = { 1 };
    >
    > will initialize the first element of i to 1 and the rest to 0.
    > (And if you use 0 as the only initializer you will get an array
    > with all 10 elements set to 0.)


    While you're both right (unless I too am mistaken!) I think Rox's
    result is more surprising. I think one would expect initializing an
    int array to initialize all the elements of that array. Whereas, if a
    character array is intended to be initialised to a particular string,
    one might expect that putting all the characters of the string into
    the array, plus a zero to show the end, to be a sufficient
    initialisation; one might not expect it to fill in all the other chars
    as well.
     
    Paul N, Aug 12, 2011
    #3
  4. "Rox" <> writes:
    > For array initialization, I found this in [n1539] 6.7.9

    [snip]

    Keep in mind that n1539 is a draft of the upcoming C201X standard. It's
    very useful if you want to see what's coming in future versions of the
    language, but it describes a version of C that isn't yet official and
    has not yet been implemented.

    For the current version of the language, see
    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 12, 2011
    #4
  5. Rox

    James Kuyper Guest

    On 08/12/2011 11:47 AM, Keith Thompson wrote:
    > "Rox" <> writes:
    >> For array initialization, I found this in [n1539] 6.7.9

    > [snip]
    >
    > Keep in mind that n1539 is a draft of the upcoming C201X standard. It's
    > very useful if you want to see what's coming in future versions of the
    > language, but it describes a version of C that isn't yet official and
    > has not yet been implemented.
    >
    > For the current version of the language, see
    > <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>.


    This particular feature, however, has been supported for decades. I
    believe it predates the first version of the C standard, which came out
    in 1989.
     
    James Kuyper, Aug 12, 2011
    #5
  6. Rox

    Alan Curry Guest

    In article <-berlin.de>,
    Jens Thoms Toerring <> wrote:
    >Rox <> wrote:
    >> So I presume:

    >
    >> char c[10] = "";

    >
    >> Will initialize c into 0. Just like memset do.
    >> It's such a neat way to initial a string that I can't believe it!
    >> Am I right?

    >
    >Yes, it has been that way for a long time. In C89 (3.5.7) you
    >have:
    >
    >| If there are fewer initializers in a list than there are members of
    >| an aggregate, the remainder of the aggregate shall be initialized
    >| implicitly the same as objects that have static storage duration.


    Which I look at as a historical misfeature; there's no way to initialize
    a local string without also wasting a bunch of cycles zero-filling the
    bytes after the terminator. It would have been nice if there was a way
    to write an initializer equivalent to

    char buf[4096];
    strcpy(buf, "some short string");

    --
    Alan Curry
     
    Alan Curry, Aug 12, 2011
    #6
  7. Alan Curry <> wrote:
    > In article <-berlin.de>,
    > Jens Thoms Toerring <> wrote:
    > >Rox <> wrote:
    > >> So I presume:

    > >
    > >> char c[10] = "";

    > >
    > >> Will initialize c into 0. Just like memset do.
    > >> It's such a neat way to initial a string that I can't believe it!
    > >> Am I right?

    > >
    > >Yes, it has been that way for a long time. In C89 (3.5.7) you
    > >have:
    > >
    > >| If there are fewer initializers in a list than there are members of
    > >| an aggregate, the remainder of the aggregate shall be initialized
    > >| implicitly the same as objects that have static storage duration.


    > Which I look at as a historical misfeature; there's no way to initialize
    > a local string without also wasting a bunch of cycles zero-filling the
    > bytes after the terminator. It would have been nice if there was a way
    > to write an initializer equivalent to


    > char buf[4096];
    > strcpy(buf, "some short string");


    My (uneducated) guess is that the idea might have been not to
    allow a "partial initialization" of an object, i.e. either don't
    initialize it at all or do a complete job of it. Looks not so
    reasonable for a char array (if it's used for a possibly shorter
    string) but more sensible for other kinds of arrays (or struc-
    tures) - and then having another special case for char arrays
    would have complicated things unnecessary when a strcpy() or
    memcpy() can be used for that purpose rather easily. But, as
    I said, this is pure guesswork...

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Aug 12, 2011
    #7
  8. Rox

    Ian Collins Guest

    On 08/13/11 10:33 AM, Alan Curry wrote:
    > In article<-berlin.de>,
    > Jens Thoms Toerring<> wrote:
    >> Rox<> wrote:
    >>> So I presume:

    >>
    >>> char c[10] = "";

    >>
    >>> Will initialize c into 0. Just like memset do.
    >>> It's such a neat way to initial a string that I can't believe it!
    >>> Am I right?

    >>
    >> Yes, it has been that way for a long time. In C89 (3.5.7) you
    >> have:
    >>
    >> | If there are fewer initializers in a list than there are members of
    >> | an aggregate, the remainder of the aggregate shall be initialized
    >> | implicitly the same as objects that have static storage duration.

    >
    > Which I look at as a historical misfeature; there's no way to initialize
    > a local string without also wasting a bunch of cycles zero-filling the
    > bytes after the terminator. It would have been nice if there was a way
    > to write an initializer equivalent to
    >
    > char buf[4096];
    > strcpy(buf, "some short string");


    You have your answer - the code above!

    How often in real code would you see

    char buf[4096] = "some short string";

    ?

    --
    Ian Collins
     
    Ian Collins, Aug 13, 2011
    #8
  9. Rox

    Rox Guest

    And from n1256, I got the same conclusion.

    That's a lot.

    Rox

    "Keith Thompson" written message news:...

    "Rox" <> writes:
    > For array initialization, I found this in [n1539] 6.7.9

    [snip]

    Keep in mind that n1539 is a draft of the upcoming C201X standard. It's
    very useful if you want to see what's coming in future versions of the
    language, but it describes a version of C that isn't yet official and
    has not yet been implemented.

    For the current version of the language, see
    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Rox, Aug 15, 2011
    #9
    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. Petter Reinholdtsen

    char array initialization: Is 'char a[] = ("a")' valid ANSI C?

    Petter Reinholdtsen, Nov 1, 2004, in forum: C Programming
    Replies:
    20
    Views:
    1,088
    Dave Thompson
    Nov 22, 2004
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,132
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    755
  4. davidb
    Replies:
    0
    Views:
    803
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,581
    Default User
    Sep 1, 2006
Loading...

Share This Page