struct initializer { 0 }

Discussion in 'C Programming' started by M Kumar, Sep 28, 2003.

  1. M Kumar

    M Kumar Guest

    Suppose I have a struct:

    struct ncb
    {
    int a, b, c, d;
    long e, f, g, h;
    unsigned long i, j, k, l;
    unsigned char buf[20];
    };

    can I use

    struct ncb n1 = { 0 };

    to set it to zero? Is this valid/legal C?

    TIA,
    K
    M Kumar, Sep 28, 2003
    #1
    1. Advertising

  2. M Kumar

    rihad Guest

    On 28 Sep 2003 11:05:18 -0700, (M Kumar) wrote:

    >Suppose I have a struct:
    >
    >struct ncb
    >{
    > int a, b, c, d;
    > long e, f, g, h;
    > unsigned long i, j, k, l;
    > unsigned char buf[20];
    >};
    >
    >can I use
    >
    >struct ncb n1 = { 0 };
    >
    >to set it to zero? Is this valid/legal C?
    >


    Sure! It's much more C than the dreaded memset(&n1, 0, sizeof ncb); used so
    frequently in Windows programming.
    rihad, Sep 28, 2003
    #2
    1. Advertising

  3. "M Kumar" <> wrote in message
    news:...
    > Suppose I have a struct:
    >
    > struct ncb
    > {
    > int a, b, c, d;
    > long e, f, g, h;
    > unsigned long i, j, k, l;
    > unsigned char buf[20];
    > };
    >
    > can I use
    >
    > struct ncb n1 = { 0 };
    >
    > to set it to zero? Is this valid/legal C?


    Yes. All members will be set to zero (or NULL).

    hth-Ivan
    --
    http://ivan.vecerina.com
    Ivan Vecerina, Sep 28, 2003
    #3
    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. Chris Fogelklou
    Replies:
    36
    Views:
    1,345
    Chris Fogelklou
    Apr 20, 2004
  2. Laurent Deniau

    struct initializer efficiency

    Laurent Deniau, Aug 28, 2007, in forum: C Programming
    Replies:
    0
    Views:
    261
    Laurent Deniau
    Aug 28, 2007
  3. Laurent Deniau

    struct initializer efficiency and portability

    Laurent Deniau, Aug 28, 2007, in forum: C Programming
    Replies:
    1
    Views:
    260
    Laurent Deniau
    Aug 28, 2007
  4. Richard
    Replies:
    0
    Views:
    400
    Richard
    Oct 20, 2009
  5. Thomas J. Gritzan

    Re: initializer list of the struct

    Thomas J. Gritzan, Oct 20, 2009, in forum: C++
    Replies:
    0
    Views:
    405
    Thomas J. Gritzan
    Oct 20, 2009
Loading...

Share This Page