Structure Initializers: Are Undefined Members Zero'd?

Discussion in 'C Programming' started by Michael B Allen, Sep 7, 2006.

  1. Are undefined structure members in initializers set to 0?

    Consider the following code:

    typedef struct fns {
    int (*fn1)(int n);
    int (*fn2)(int n);
    int (*fn3)(int n);
    int (*fn4)(int n);
    int (*fn5)(int n);
    } fns;
    static int
    my_fn1(int n)
    {
    ....
    }
    static int
    my_fn3(int n)
    {
    ....
    }
    const fns my_fns = {
    my_fn1,
    NULL,
    my_fn3
    };

    Are my_fns.fn4 and my_fns.fn5 guaranteed to be NULL here?

    Mike
     
    Michael B Allen, Sep 7, 2006
    #1
    1. Advertising

  2. Michael B Allen wrote:
    > Are undefined structure members in initializers set to 0?
    >
    > Consider the following code:
    >
    > typedef struct fns {
    > int (*fn1)(int n);
    > int (*fn2)(int n);
    > int (*fn3)(int n);
    > int (*fn4)(int n);
    > int (*fn5)(int n);
    > } fns;
    > static int
    > my_fn1(int n)
    > {
    > ...
    > }
    > static int
    > my_fn3(int n)
    > {
    > ...
    > }
    > const fns my_fns = {
    > my_fn1,
    > NULL,
    > my_fn3
    > };
    >
    > Are my_fns.fn4 and my_fns.fn5 guaranteed to be NULL here?
    >
    > Mike


    According to:
    http://c-faq.com/decl/initval.html
    "When an automatic array or structure has a partial initializer, the
    remainder is initialized to 0, just as for statics"
     
    markpapadakis, Sep 7, 2006
    #2
    1. Advertising

  3. On 7 Sep 2006 11:22:44 -0700, "markpapadakis" <> wrote:
    >Michael B Allen wrote:
    >> Are undefined structure members in initializers set to 0?


    Uninitialized and static, yes. Automatic and partially initialized, yes.

    >> Are my_fns.fn4 and my_fns.fn5 guaranteed to be NULL here?


    >According to:
    >http://c-faq.com/decl/initval.html
    >"When an automatic array or structure has a partial initializer, the
    >remainder is initialized to 0, just as for statics"


    More importantly, they're initialized *as if* the programmer had entered a zero constant -
    so yes, pointers thus initialized are guaranteed to be NULL.


    --
    #include <standard.disclaimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
     
    Kevin D. Quitt, Sep 7, 2006
    #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. Steven T. Hatton
    Replies:
    4
    Views:
    409
    Siemel Naran
    Aug 27, 2004
  2. John T.

    Zero elements of structure...

    John T., Aug 31, 2003, in forum: C Programming
    Replies:
    9
    Views:
    347
    Dave Thompson
    Sep 8, 2003
  3. Zhiqiang Ye
    Replies:
    53
    Views:
    10,288
    Dan Pop
    Jun 28, 2004
  4. Gerard Flanagan
    Replies:
    3
    Views:
    451
    Terry Hancock
    Nov 19, 2005
  5. Christopher Benson-Manica

    Doubles and zero/negative zero

    Christopher Benson-Manica, Jun 30, 2004, in forum: C Programming
    Replies:
    4
    Views:
    683
    Walter
    Jul 1, 2004
Loading...

Share This Page