initializing arrays of arrays

Discussion in 'C Programming' started by Mantorok Redgormor, Sep 10, 2003.

  1. Is this legal?

    int foo[10][10] = { 0 };

    gcc gives:

    foo.c: In function `main':
    foo.c:5: warning: missing braces around initializer
    foo.c:5: warning: (near initialization for `foo[0]')
    foo.c:5: warning: unused variable `foo'

    And what part of the standard is it explained?
     
    Mantorok Redgormor, Sep 10, 2003
    #1
    1. Advertising

  2. Mantorok Redgormor

    Kevin Easton Guest

    Mantorok Redgormor <> wrote:
    > Is this legal?
    >
    > int foo[10][10] = { 0 };


    Yes.

    > gcc gives:
    >
    > foo.c: In function `main':
    > foo.c:5: warning: missing braces around initializer
    > foo.c:5: warning: (near initialization for `foo[0]')


    gcc is trying to be helpful, but in this case it failed.

    > And what part of the standard is it explained?


    Section 6.7.8 in C99

    - Kevin.
     
    Kevin Easton, Sep 10, 2003
    #2
    1. Advertising

  3. Mantorok Redgormor

    Jirka Klaue Guest

    Mantorok Redgormor wrote:
    > Is this legal?
    >
    > int foo[10][10] = { 0 };


    It is legal.

    > gcc gives:
    >
    > foo.c: In function `main':
    > foo.c:5: warning: missing braces around initializer


    Gcc tells you that it wants { {0} }.
    It is free to do so, but { 0 } is correct nevertheless.

    > And what part of the standard is it explained?


    ISO/IEC 9899:1999
    6.7.8 Initialization, especially clause 21.

    Jirka
     
    Jirka Klaue, Sep 10, 2003
    #3
  4. Mantorok Redgormor

    Al Bowers Guest

    Mantorok Redgormor wrote:

    > Is this legal?
    >
    > int foo[10][10] = { 0 };
    >
    > gcc gives:
    >
    > foo.c: In function `main':
    > foo.c:5: warning: missing braces around initializer
    > foo.c:5: warning: (near initialization for `foo[0]')
    > foo.c:5: warning: unused variable `foo'
    >
    > And what part of the standard is it explained?



    You compiler is set for a high level of warning like -Wall.
    So you are getting warnings of things that are ok but might possibly
    be wrong. In this cause the warning is not fruitful as the code is
    ok.

    In the standard:

    The Standard says:

    6.7.8.21
    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 knownsize 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.

    And for static storage:

    6.7.8.10
    If an object that has static storage duration is not initialized explicitly,
    then:
    — if it has pointer type, it is initialized to a null pointer;
    — if it has arithmetic type, it is initialized to (positive or unsigned)
    zero;
    — if it is an aggregate, every member is initialized (recursively)
    according to these rules;
    — if it is a union, the first named member is initialized (recursively)
    according to these rules.

    So, int foo[10][10] = {0}; is ok.

    Your warning probably came from

    6.7.8.20
    If the aggregate or union contains elements or members that are
    aggregates or unions, these rules apply recursively to the
    subaggregates or contained unions. If the initializer of
    a subaggregate or contained union begins with a left brace, the
    initializers enclosed by that brace and its matching right brace
    initialize the elements or members of the subaggregate or the
    contained union. Otherwise, only enough initializers from the list
    are taken to account for the elements or members of the subaggregate
    or the first member of the contained union; any remaining
    initializers are left to initialize the next element or member of the
    aggregate of which the current subaggregate or contained union is a
    part.

    So, you might silence the warning with:
    int foo[10][10] = { {0} };

    #include <stdio.h>

    int main(void)
    {
    int i,j, array[10][10] = {{0}};

    for(i = 0;i < 10;i++)
    {
    for(j = 0; j < 10;j++)
    printf(" %d",array[j]);
    putchar('\n');
    }
    return 0;
    }

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Sep 10, 2003
    #4
  5. Mantorok Redgormor

    Jeff Guest

    "Mantorok Redgormor" <> wrote in message
    news:...
    > Is this legal?
    >
    > int foo[10][10] = { 0 };
    >


    legal.

    When you only initialize first few elements in the array, the rest of them
    will be initialized to zero.

    > gcc gives:
    >
    > foo.c: In function `main':
    > foo.c:5: warning: missing braces around initializer
    > foo.c:5: warning: (near initialization for `foo[0]')
    > foo.c:5: warning: unused variable `foo'
    >
    > And what part of the standard is it explained?


    6.7.8


    --
    Jeff

    je6543 at yahoo dot com
     
    Jeff, Sep 11, 2003
    #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. Klaus Rudolph

    initializing member arrays

    Klaus Rudolph, Jan 15, 2004, in forum: C++
    Replies:
    3
    Views:
    2,219
    Jerry Coffin
    Jan 18, 2004
  2. Allan A.
    Replies:
    3
    Views:
    669
    mlimber
    Feb 22, 2006
  3. John
    Replies:
    13
    Views:
    614
    Jerry Coffin
    Sep 24, 2006
  4. Victor Bazarov

    Re: Initializing arrays of objects

    Victor Bazarov, Oct 21, 2009, in forum: C++
    Replies:
    1
    Views:
    1,778
    Ross A. Finlayson
    Oct 21, 2009
  5. Van Jacques
    Replies:
    10
    Views:
    207
    Van Jacques
    Dec 6, 2003
Loading...

Share This Page