incomplete initialization of array

Discussion in 'C Programming' started by Mark, Sep 30, 2011.

  1. Mark

    Mark Guest

    Hi all :

    I thought that the following code will initialilze every element of array,
    but it appears that it does so only for the 0th index.

    #define MAX_LEN 10
    char name[MAX_LEAN + 1] = { '\0', };

    Somehow I was assured that such syntax, i.e. value followed by comma will
    work for all elements of arrays. Seems I was wrong and only memset() will do
    what I need. Does the standard clearly says that such construction will
    never work?

    Thanks !

    Mark
     
    Mark, Sep 30, 2011
    #1
    1. Advertising

  2. Mark

    Ben Pfaff Guest

    "Mark" <> writes:

    > I thought that the following code will initialilze every element of array,
    > but it appears that it does so only for the 0th index.
    >
    > #define MAX_LEN 10
    > char name[MAX_LEAN + 1] = { '\0', };


    Assuming that MAX_LEAN is a typo for MAX_LEN, this will
    initialize all 11 elements to 0.

    C99 says:

    If there are fewer initializers in a brace-enclosed list than there
    are elements or members of an aggregate, or fewer characters in a
    string literal used to initialize an array of known size than there
    are elements in the array, the remainder of the aggregate shall be
    initialized implicitly the same as objects that have static storage
    duration.

    --
    "Programmers have the right to be ignorant of many details of your code
    and still make reasonable changes."
    --Kernighan and Plauger, _Software Tools_
     
    Ben Pfaff, Sep 30, 2011
    #2
    1. Advertising

  3. "Mark" <> writes:

    > I thought that the following code will initialilze every element of array,
    > but it appears that it does so only for the 0th index.
    >
    > #define MAX_LEN 10
    > char name[MAX_LEAN + 1] = { '\0', };
    >
    > Somehow I was assured that such syntax, i.e. value followed by comma will
    > work for all elements of arrays. Seems I was wrong and only memset() will do
    > what I need. Does the standard clearly says that such construction will
    > never work?


    As the other Ben has said, it will work. The comma has nothing to do
    with it, by the way. It would be interesting to know what has made you
    think it is not working. That is where the problem lies, not with the
    array declaration.

    --
    Ben.
     
    Ben Bacarisse, Sep 30, 2011
    #3
  4. Mark wrote:
    >
    > I thought that the following code will initialilze every element of array,


    You thought correctly.

    > but it appears that it does so only for the 0th index.


    How does that appear? Something else must be happening between the
    initialization and the appearance.

    > #define MAX_LEN 10
    > char name[MAX_LEAN + 1] = { '\0', };


    If this is the actual code rather than a typo, then the array is only
    one element in length. Is that what has confused you?

    > Somehow I was assured that such syntax, i.e. value followed by comma will
    > work for all elements of arrays.


    The comma has nothing to do with it. You are allowed to leave a comma on
    the end of a list of initializers, but it doesn't change anything about
    the initialization.

    > Seems I was wrong and only memset() will do what I need.


    No, you were more or less right.

    > Does the standard clearly says that such construction will never work?


    No, the Standard clearly says that such a construction must always work.
    The comma is redundant and looks a bit silly to me, I'd leave it off.
     
    J. J. Farrell, Oct 1, 2011
    #4
  5. On Oct 1, 4:53 am, "J. J. Farrell" <> wrote:
    > Mark wrote:
    > > #define MAX_LEN 10
    > > char name[MAX_LEAN + 1] = { '\0', };

    >
    > If this is the actual code rather than a typo, then the array is only
    > one element in length. Is that what has confused you?


    I don't understand what you mean here. Yes, MAX_LEN and MAX_LEAN are
    spelt differently, but that should result in a compiler error, not an
    array of a different length.
     
    Harald van Dijk, Oct 1, 2011
    #5
  6. Mark

    BartC Guest

    "Mark" <> wrote in message
    news:j64u3k$op3$...
    > Hi all :
    >
    > I thought that the following code will initialilze every element of array,
    > but it appears that it does so only for the 0th index.
    >
    > #define MAX_LEN 10
    > char name[MAX_LEAN + 1] = { '\0', };
    >
    > Somehow I was assured that such syntax, i.e. value followed by comma will
    > work for all elements of arrays. Seems I was wrong and only memset() will
    > do what I need. Does the standard clearly says that such construction will
    > never work?


    I don't know about the standard. But some experimentation shows only the
    first element is set. The rest seem to be zeros.

    If you're only interested in zeros anyway, then it doesn't matter.

    --
    Bartc
     
    BartC, Oct 1, 2011
    #6
  7. "BartC" <> writes:

    > "Mark" <> wrote in message
    > news:j64u3k$op3$...
    >> Hi all :
    >>
    >> I thought that the following code will initialilze every element of
    >> array, but it appears that it does so only for the 0th index.
    >>
    >> #define MAX_LEN 10
    >> char name[MAX_LEAN + 1] = { '\0', };
    >>
    >> Somehow I was assured that such syntax, i.e. value followed by comma
    >> will work for all elements of arrays. Seems I was wrong and only
    >> memset() will do what I need. Does the standard clearly says that
    >> such construction will never work?

    >
    > I don't know about the standard. But some experimentation shows only
    > the first element is set. The rest seem to be zeros.


    But in the OP's case the explicit initialiser is also zero so maybe some
    of the answers might have been a little confusing. "It works" means
    that the code does what the OP seems to want, but it does not mean that
    an array can be initialised with all non-zero values by simply giving
    one non-zero initialiser.

    The standard says that the remaining elements are initialised as if the
    object had static storage duration -- that's a shorthand for zero (the
    element type's zero value, not all-bits-zero though they may be the
    same). Any non-zero elements have to be explicitly set.

    --
    Ben.
     
    Ben Bacarisse, Oct 1, 2011
    #7
  8. Mark

    Joe Pfeiffer Guest

    "BartC" <> writes:

    > "Mark" <> wrote in message
    > news:j64u3k$op3$...
    >> Hi all :
    >>
    >> I thought that the following code will initialilze every element of
    >> array, but it appears that it does so only for the 0th index.
    >>
    >> #define MAX_LEN 10
    >> char name[MAX_LEAN + 1] = { '\0', };
    >>
    >> Somehow I was assured that such syntax, i.e. value followed by comma
    >> will work for all elements of arrays. Seems I was wrong and only
    >> memset() will do what I need. Does the standard clearly says that
    >> such construction will never work?

    >
    > I don't know about the standard. But some experimentation shows only
    > the first element is set. The rest seem to be zeros.


    Well, yes -- if you specify any at all, the ones you specify are set to
    whatever you want. The others are all set to 0.

    > If you're only interested in zeros anyway, then it doesn't matter.


    It matters, since otherwise the contents of the array are effectively
    random.
     
    Joe Pfeiffer, Oct 1, 2011
    #8
  9. Mark

    BartC Guest

    "Joe Pfeiffer" <> wrote in message
    news:...
    > "BartC" <> writes:


    >> I don't know about the standard. But some experimentation shows only
    >> the first element is set. The rest seem to be zeros.

    >
    > Well, yes -- if you specify any at all, the ones you specify are set to
    > whatever you want. The others are all set to 0.
    >
    >> If you're only interested in zeros anyway, then it doesn't matter.

    >
    > It matters, since otherwise the contents of the array are effectively
    > random.


    I meant that, if element 0 *was* initialised to zero, the other elements are
    coincidentally set to zero as well.

    This won't work for a value other than zero, as it wouldn't be propagated
    into the remaining elements as seemed (to me) to be implied if you didn't
    read the other replies carefully:

    int a[5]={0}; (0,0,0,0,0) as expected
    int a[5]={1}; (1,0,0,0,0) not (1,1,1,1,1) as might be assumed.

    --
    Bartc
     
    BartC, Oct 2, 2011
    #9
  10. Mark

    James Kuyper Guest

    On 10/01/2011 07:55 PM, BartC wrote:
    > "Joe Pfeiffer" <> wrote in message
    > news:...
    >> "BartC" <> writes:

    >
    >>> I don't know about the standard. But some experimentation shows only
    >>> the first element is set. The rest seem to be zeros.

    >>
    >> Well, yes -- if you specify any at all, the ones you specify are set to
    >> whatever you want. The others are all set to 0.

    ....
    > int a[5]={1}; (1,0,0,0,0) not (1,1,1,1,1) as might be assumed.


    You're right, that assumption is incorrect. But I didn't realize that's
    what you were talking about. Is that what Mark was talking about, too?
    --
    James Kuyper
     
    James Kuyper, Oct 2, 2011
    #10
  11. Harald van Dijk wrote:
    > On Oct 1, 4:53 am, "J. J. Farrell" <> wrote:
    >> Mark wrote:
    >>> #define MAX_LEN 10
    >>> char name[MAX_LEAN + 1] = { '\0', };

    >> If this is the actual code rather than a typo, then the array is only
    >> one element in length. Is that what has confused you?

    >
    > I don't understand what you mean here. Yes, MAX_LEN and MAX_LEAN are
    > spelt differently, but that should result in a compiler error, not an
    > array of a different length.


    Apologies, I must have been asleep when posting. My mind had wandered
    off into the messy areas of preprocessor constant expressions involving
    macros which may or may not be defined, and may be defined as empty. I
    had to fight with that a lot in my first C project, and it left deep
    scars. I don't know which genius added "defined" to C89, but I remember
    wanting to buy him a pint; it made that sort of thing much easier.

    My comment would have made sense if there were a

    #define MAX_LEAN

    or equivalent before that code.
     
    J. J. Farrell, Oct 2, 2011
    #11
  12. James Kuyper wrote:
    > On 10/01/2011 07:55 PM, BartC wrote:
    >> "Joe Pfeiffer" <> wrote in message
    >> news:...
    >>> "BartC" <> writes:
    >>>> I don't know about the standard. But some experimentation shows only
    >>>> the first element is set. The rest seem to be zeros.
    >>> Well, yes -- if you specify any at all, the ones you specify are set to
    >>> whatever you want. The others are all set to 0.

    > ...
    >> int a[5]={1}; (1,0,0,0,0) not (1,1,1,1,1) as might be assumed.

    >
    > You're right, that assumption is incorrect. But I didn't realize that's
    > what you were talking about. Is that what Mark was talking about, too?


    Hard to tell since the example he gave would have had the same result
    for both possibilities. For the original example, at least, "not
    working" couldn't mean he'd made the wrong assumption described here.
     
    J. J. Farrell, Oct 2, 2011
    #12
    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. Paul F. Dietz

    Question about incomplete array element types

    Paul F. Dietz, Jul 11, 2003, in forum: C Programming
    Replies:
    5
    Views:
    458
    Dan Pop
    Jul 11, 2003
  2. Eric Smith

    structure containing incomplete array type

    Eric Smith, Apr 9, 2004, in forum: C Programming
    Replies:
    6
    Views:
    3,441
    Kevin Bracey
    Apr 13, 2004
  3. toton
    Replies:
    5
    Views:
    935
    Victor Bazarov
    Sep 28, 2006
  4. Replies:
    1
    Views:
    964
    Richard Bos
    Jan 17, 2008
  5. aaragon
    Replies:
    2
    Views:
    621
    James Kanze
    Nov 2, 2008
Loading...

Share This Page