Structure Initializers: Are Undefined Members Zero'd?

  • Thread starter Michael B Allen
  • Start date
M

Michael B Allen

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
 
M

markpapadakis

Michael said:
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"
 
K

Kevin D. Quitt

Uninitialized and static, yes. Automatic and partially initialized, yes.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top