# Structure Initializers: Are Undefined Members Zero'd?

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

1. ### Michael B AllenGuest

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

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"

3. ### Kevin D. QuittGuest

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