howto init 2d structure to zero ?

Discussion in 'C Programming' started by Stef, May 24, 2005.

  1. Stef

    Stef Guest

    Hello,

    I have a question:

    Is it possible to init a 2d array of structures to zero ?

    For example with array I do.
    int array[MAX] = {0}

    but:
    structure test bb[MAX][MAX] = {{0},{0}};

    won't work.. ?

    thank yoo.

    Stef.
    Stef, May 24, 2005
    #1
    1. Advertising

  2. Stef

    bjrnove Guest

    Hi.

    This should work thow:
    struct test bb[MAX][MAX] = {0};

    If you put = {0}; as the initializer it will alway initialize the
    entire thing to \0's.

    --
    bjrnove
    bjrnove, May 24, 2005
    #2
    1. Advertising

  3. Stef

    Stef Guest

    On 24 May 2005 03:32:09 -0700, "bjrnove" <> wrote:

    > Hi.
    >
    > This should work thow:
    > struct test bb[MAX][MAX] = {0};
    >
    > If you put = {0}; as the initializer it will alway initialize the
    > entire thing to \0's.


    But the compiler gives warnings then... like:
    istrct.c: In function `main':
    istrct.c:11: warning: missing braces around initializer
    istrct.c:11: warning: (near initialization for `aaaa[0]')
    istrct.c:12: warning: missing braces around initializer
    istrct.c:12: warning: (near initialization for `bbbb[0]')
    istrct.c:12: warning: unused variable `bbbb'
    istrct.c:11: warning: unused variable `aaaa'

    For completeness Here is small simple skeleton..
    #include <stdio.h>

    #define MAXTAB 100
    #define MAXCOL 3

    struct tab {
    double freq;
    };

    int main(void) {
    struct tab aaaa[MAXTAB][MAXCOL] = {0};
    struct tab bbbb[MAXTAB][MAXCOL] = {0};

    return 0;
    }
    Stef, May 24, 2005
    #3
  4. Stef

    -berlin.de Guest

    Stef <> wrote:
    > On 24 May 2005 03:32:09 -0700, "bjrnove" <> wrote:


    >> Hi.
    >>
    >> This should work thow:
    >> struct test bb[MAX][MAX] = {0};
    >>
    >> If you put = {0}; as the initializer it will alway initialize the
    >> entire thing to \0's.


    > But the compiler gives warnings then... like:
    > istrct.c: In function `main':
    > istrct.c:11: warning: missing braces around initializer
    > istrct.c:11: warning: (near initialization for `aaaa[0]')
    > istrct.c:12: warning: missing braces around initializer
    > istrct.c:12: warning: (near initialization for `bbbb[0]')
    > istrct.c:12: warning: unused variable `bbbb'
    > istrct.c:11: warning: unused variable `aaaa'


    > For completeness Here is small simple skeleton..
    > #include <stdio.h>


    > #define MAXTAB 100
    > #define MAXCOL 3


    > struct tab {
    > double freq;
    > };


    > int main(void) {
    > struct tab aaaa[MAXTAB][MAXCOL] = {0};
    > struct tab bbbb[MAXTAB][MAXCOL] = {0};


    Use instead

    struct tab aaaa[MAXTAB][MAXCOL] = {{{0}}};

    If you have e.g. a 2-dimensional arrays of ints you would do e.g.

    int foo[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

    and since the initializer for a structure needs curly braces you need
    another level of them for your 2-dimensional array of structures.

    If the structure has more than a single member put in a value for
    each member, i.e.

    struct tab2 {
    double freq;
    int x;
    }

    struct tab2 ccc[MAXTAB][MAXCOL] = {{{0,0}}};

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, May 24, 2005
    #4
  5. Stef

    S.Tobias Guest

    Stef <> wrote:

    > For example with array I do.
    > int array[MAX] = {0}


    > but:
    > structure test bb[MAX][MAX] = {{0},{0}};


    > won't work.. ?


    What makes you think it won't work, assuming all necessary
    definitions are provided?

    This compiled cleanly:
    #define MAX 10
    struct test { int x; };
    struct test bb[MAX][MAX] = {{0},{0}};

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, May 24, 2005
    #5
  6. Stef

    Stef Guest

    On 24 May 2005 10:54:17 GMT, -berlin.de wrote:

    > Stef <> wrote:
    > > On 24 May 2005 03:32:09 -0700, "bjrnove" <> wrote:

    >
    > >> Hi.
    > >>
    > >> This should work thow:
    > >> struct test bb[MAX][MAX] = {0};
    > >>
    > >> If you put = {0}; as the initializer it will alway initialize the
    > >> entire thing to \0's.

    >
    > > But the compiler gives warnings then... like:
    > > istrct.c: In function `main':
    > > istrct.c:11: warning: missing braces around initializer
    > > istrct.c:11: warning: (near initialization for `aaaa[0]')
    > > istrct.c:12: warning: missing braces around initializer
    > > istrct.c:12: warning: (near initialization for `bbbb[0]')
    > > istrct.c:12: warning: unused variable `bbbb'
    > > istrct.c:11: warning: unused variable `aaaa'

    >
    > > For completeness Here is small simple skeleton..
    > > #include <stdio.h>

    >
    > > #define MAXTAB 100
    > > #define MAXCOL 3

    >
    > > struct tab {
    > > double freq;
    > > };

    >
    > > int main(void) {
    > > struct tab aaaa[MAXTAB][MAXCOL] = {0};
    > > struct tab bbbb[MAXTAB][MAXCOL] = {0};

    >
    > Use instead
    >
    > struct tab aaaa[MAXTAB][MAXCOL] = {{{0}}};
    >
    > If you have e.g. a 2-dimensional arrays of ints you would do e.g.
    >
    > int foo[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
    >
    > and since the initializer for a structure needs curly braces you need
    > another level of them for your 2-dimensional array of structures.
    >
    > If the structure has more than a single member put in a value for
    > each member, i.e.
    >
    > struct tab2 {
    > double freq;
    > int x;
    > }
    >
    > struct tab2 ccc[MAXTAB][MAXCOL] = {{{0,0}}};
    >
    > Regards, Jens


    Ok thank yoo .. That worked !

    Stef
    Stef, May 24, 2005
    #6
  7. Stef

    Stef Guest

    On 24 May 2005 11:06:31 GMT, "S.Tobias"
    <> wrote:

    > Stef <> wrote:
    >
    > > For example with array I do.
    > > int array[MAX] = {0}

    >
    > > but:
    > > structure test bb[MAX][MAX] = {{0},{0}};

    >
    > > won't work.. ?

    >
    > What makes you think it won't work, assuming all necessary
    > definitions are provided?
    >
    > This compiled cleanly:
    > #define MAX 10
    > struct test { int x; };
    > struct test bb[MAX][MAX] = {{0},{0}};


    ~: cat strct.c
    #include <stdio.h>

    #define MAX 10

    int main(void) {
    struct test { int x; };
    struct test bb[MAX][MAX] = {{0},{0}};

    return 0;
    }

    ~: gcc -Wall -pedantic strct.c -o strct
    strct.c: In function `main':
    strct.c:7: warning: missing braces around initializer
    strct.c:7: warning: (near initialization for `bb[0][0]')
    strct.c:7: warning: unused variable `bb'

    That made me thinc it won't worked

    Stef.
    Stef, May 24, 2005
    #7
  8. Stef

    S.Tobias Guest

    Stef <> wrote:
    > On 24 May 2005 03:32:09 -0700, "bjrnove" <> wrote:


    > > This should work thow:
    > > struct test bb[MAX][MAX] = {0};
    > >
    > > If you put = {0}; as the initializer it will alway initialize the
    > > entire thing to \0's.


    > But the compiler gives warnings then... like:
    > istrct.c: In function `main':
    > istrct.c:11: warning: missing braces around initializer
    > istrct.c:11: warning: (near initialization for `aaaa[0]')
    > istrct.c:12: warning: missing braces around initializer
    > istrct.c:12: warning: (near initialization for `bbbb[0]')
    > istrct.c:12: warning: unused variable `bbbb'
    > istrct.c:11: warning: unused variable `aaaa'


    > For completeness Here is small simple skeleton..
    > #include <stdio.h>


    > #define MAXTAB 100
    > #define MAXCOL 3


    > struct tab {
    > double freq;
    > };


    > int main(void) {
    > struct tab aaaa[MAXTAB][MAXCOL] = {0};
    > struct tab bbbb[MAXTAB][MAXCOL] = {0};


    > return 0;
    > }


    The program is correct. Compiler may issue as many warnings it
    feels like to. The extra warnings that you see are invoked
    by "-Wall" option to gcc (since we already have "-pedantic", this
    one could be called "-paranoic"); look into the gcc documentation.

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, May 24, 2005
    #8
  9. Stef

    Stef Guest

    On 24 May 2005 11:14:49 GMT, "S.Tobias"
    <> wrote:

    > Stef <> wrote:
    > > On 24 May 2005 03:32:09 -0700, "bjrnove" <> wrote:

    >
    > > > This should work thow:
    > > > struct test bb[MAX][MAX] = {0};
    > > >
    > > > If you put = {0}; as the initializer it will alway initialize the
    > > > entire thing to \0's.

    >
    > > But the compiler gives warnings then... like:
    > > istrct.c: In function `main':
    > > istrct.c:11: warning: missing braces around initializer
    > > istrct.c:11: warning: (near initialization for `aaaa[0]')
    > > istrct.c:12: warning: missing braces around initializer
    > > istrct.c:12: warning: (near initialization for `bbbb[0]')
    > > istrct.c:12: warning: unused variable `bbbb'
    > > istrct.c:11: warning: unused variable `aaaa'

    >
    > > For completeness Here is small simple skeleton..
    > > #include <stdio.h>

    >
    > > #define MAXTAB 100
    > > #define MAXCOL 3

    >
    > > struct tab {
    > > double freq;
    > > };

    >
    > > int main(void) {
    > > struct tab aaaa[MAXTAB][MAXCOL] = {0};
    > > struct tab bbbb[MAXTAB][MAXCOL] = {0};

    >
    > > return 0;
    > > }

    >
    > The program is correct. Compiler may issue as many warnings it
    > feels like to. The extra warnings that you see are invoked
    > by "-Wall" option to gcc (since we already have "-pedantic", this
    > one could be called "-paranoic"); look into the gcc documentation.


    I know what flags I am passing, even tho the program might be correct
    I still feel it's proper custom to have code compiled cleanly without
    any warnings..

    Stef
    Stef, May 24, 2005
    #9
  10. Stef

    Flash Gordon Guest

    Stef wrote:
    > Hello,
    >
    > I have a question:
    >
    > Is it possible to init a 2d array of structures to zero ?
    >
    > For example with array I do.
    > int array[MAX] = {0}
    >
    > but:
    > structure test bb[MAX][MAX] = {{0},{0}};
    >
    > won't work.. ?


    You can just use:
    structure test bb[MAX][MAX] = {0};
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, May 24, 2005
    #10
  11. -berlin.de wrote:

    > Stef <> wrote:
    >> On 24 May 2005 03:32:09 -0700, "bjrnove" <> wrote:

    >
    >>> Hi.
    >>>
    >>> This should work thow:
    >>> struct test bb[MAX][MAX] = {0};
    >>>
    >>> If you put = {0}; as the initializer it will alway initialize the
    >>> entire thing to \0's.

    >
    >> But the compiler gives warnings then... like:
    >> istrct.c: In function `main':
    >> istrct.c:11: warning: missing braces around initializer
    >> istrct.c:11: warning: (near initialization for `aaaa[0]')
    >> istrct.c:12: warning: missing braces around initializer
    >> istrct.c:12: warning: (near initialization for `bbbb[0]')
    >> istrct.c:12: warning: unused variable `bbbb'
    >> istrct.c:11: warning: unused variable `aaaa'

    >
    >> For completeness Here is small simple skeleton..
    >> #include <stdio.h>

    >
    >> #define MAXTAB 100
    >> #define MAXCOL 3

    >
    >> struct tab {
    >> double freq;
    >> };

    >
    >> int main(void) {
    >> struct tab aaaa[MAXTAB][MAXCOL] = {0};
    >> struct tab bbbb[MAXTAB][MAXCOL] = {0};

    >
    > Use instead
    >
    > struct tab aaaa[MAXTAB][MAXCOL] = {{{0}}};
    >
    > If you have e.g. a 2-dimensional arrays of ints you would do e.g.
    >
    > int foo[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
    >
    > and since the initializer for a structure needs curly braces you need
    > another level of them for your 2-dimensional array of structures.
    >
    > If the structure has more than a single member put in a value for
    > each member, i.e.
    >
    > struct tab2 {
    > double freq;
    > int x;
    > }
    >
    > struct tab2 ccc[MAXTAB][MAXCOL] = {{{0,0}}};
    >
    > Regards, Jens

    Even int the case of a struct with more than one component,
    this should be enough to init the whole array to "zero"

    struct tab2 ccc[MAXTAB][MAXCOL] = {{{ 0 }}};

    --
    Michael Knaup
    Michael Knaup, May 24, 2005
    #11
  12. Stef

    Flash Gordon Guest

    Stef wrote:
    > On 24 May 2005 03:32:09 -0700, "bjrnove" <> wrote:
    >
    >>This should work thow:
    >>struct test bb[MAX][MAX] = {0};
    >>
    >>If you put = {0}; as the initializer it will alway initialize the
    >>entire thing to \0's.

    >
    > But the compiler gives warnings then... like:
    > istrct.c: In function `main':
    > istrct.c:11: warning: missing braces around initializer
    > istrct.c:11: warning: (near initialization for `aaaa[0]')
    > istrct.c:12: warning: missing braces around initializer
    > istrct.c:12: warning: (near initialization for `bbbb[0]')
    > istrct.c:12: warning: unused variable `bbbb'
    > istrct.c:11: warning: unused variable `aaaa'


    The compiler is allowed to warn about anything it wants to, even
    perfectly correct C. If you are using gcc you can disable the warning,
    but the details of gcc are off topic here.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, May 24, 2005
    #12
  13. Stef

    S.Tobias Guest

    Stef <> wrote:
    > On 24 May 2005 11:14:49 GMT, "S.Tobias"
    > <> wrote:


    > > The program is correct. Compiler may issue as many warnings it
    > > feels like to. The extra warnings that you see are invoked
    > > by "-Wall" option to gcc (since we already have "-pedantic", this
    > > one could be called "-paranoic"); look into the gcc documentation.


    > I know what flags I am passing, even tho the program might be correct
    > I still feel it's proper custom to have code compiled cleanly without
    > any warnings..


    I agree. But there must be some limit to the warnings level beyond
    which they don't help you write better code, but just annoy you and
    force you to keep unnecessary clutter.
    I have tried to use "-Wall" for some time, but dropped it, because
    it didn't give me any useful information.
    For example, I often use this idiom:
    struct object { /*...*/ };
    const struct object object_zero = {0}; /* a typed "constant" */
    which is perfectly correct and clean code. With "-Wall" I would
    have to write:
    const struct object object_zero = {{{0,{0,{0,0},0},{0,{0,0}},0},.....etc}};
    which doesn't look as good for me.
    (There were also other reasons to get rid of "-Wall".)

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, May 24, 2005
    #13
  14. Stef

    Chris Torek Guest

    In article <>
    Stef <> wrote:
    >... I still feel it's proper custom to have code compiled
    >cleanly without any warnings..


    I agree with this sentiment, and in this case the "fix" was
    clear enough (put in the right number of braces), but consider:
    what will you do if you need to compile with both Compiler A
    *and* Compiler B, and code of the form:

    void f(short s) {
    ...
    }

    produces:

    warning: function f will not be compatible with K&R C

    from compiler A, so you try instead:

    void f(int s0) {
    short s = s0;
    ...
    }

    which produces:

    warning: assignment to s may truncate s0

    from Compiler B, so you try something else (such as a cast or a
    mask), but no matter what you do, you always get at least one
    warning from at least one of the two compilers?

    In extreme cases, you might even find that a single compiler produces
    a warning no matter what you do. Sometimes you have to live with
    warnings. :)

    Ideally, there is some portable and clear way to write the code
    that is warning-free, but sometimes "there just ain't". Occasionally
    one can get rid of a warning by writing nonportable, convoluted,
    fragile code instead of portable, simple, robust code; in this
    case, eliminating the warning is not a good idea after all.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, May 24, 2005
    #14
  15. Stef

    Stef Guest

    On 24 May 2005 16:29:10 GMT, Chris Torek <> wrote:

    > In article <>
    > Stef <> wrote:
    > >... I still feel it's proper custom to have code compiled
    > >cleanly without any warnings..

    >
    > I agree with this sentiment, and in this case the "fix" was
    > clear enough (put in the right number of braces), but consider:
    > what will you do if you need to compile with both Compiler A
    > *and* Compiler B, and code of the form:
    >
    > void f(short s) {
    > ...
    > }
    >
    > produces:
    >
    > warning: function f will not be compatible with K&R C
    >
    > from compiler A, so you try instead:
    >
    > void f(int s0) {
    > short s = s0;
    > ...
    > }
    >
    > which produces:
    >
    > warning: assignment to s may truncate s0
    >
    > from Compiler B, so you try something else (such as a cast or a
    > mask), but no matter what you do, you always get at least one
    > warning from at least one of the two compilers?
    >
    > In extreme cases, you might even find that a single compiler produces
    > a warning no matter what you do. Sometimes you have to live with
    > warnings. :)
    >
    > Ideally, there is some portable and clear way to write the code
    > that is warning-free, but sometimes "there just ain't". Occasionally
    > one can get rid of a warning by writing nonportable, convoluted,
    > fragile code instead of portable, simple, robust code; in this
    > case, eliminating the warning is not a good idea after all.


    Roger.. And thank yoo for the complete answer.

    Stef
    Stef, May 25, 2005
    #15
    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. Tony Cheng
    Replies:
    1
    Views:
    8,205
    Juan T. Llibre
    Feb 24, 2006
  2. Zhiqiang Ye
    Replies:
    53
    Views:
    10,240
    Dan Pop
    Jun 28, 2004
  3. Replies:
    1
    Views:
    656
    Jules
    Aug 18, 2005
  4. Jess
    Replies:
    4
    Views:
    438
  5. news.aon.at
    Replies:
    11
    Views:
    640
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page