missing initializer

R

Roman Mashak

Hello,

say I've a code:
....
struct device_defs {
unsigned int vendor;
unsigned int dev;
unsigned int mask;
unsigned long data;
};

static struct device_defs device_defs_tbl[] =
{
{ 0x0000, 0x0011, 0x0000, 0x0000, 0, 0, 0},
{ }
}

int main(void)
{
...
return 0;
}

What's correct way to initialize 'device_defs_tbl'? Running
'gcc -std=c99 -W -Wall' or '-ansi' both result in warnings:

t.c:13: warning: missing initializer
t.c:13: warning: (near initialization for `device_defs_tbl[1].vendor')

Thank you.

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
P

pete

Roman said:
Hello,

say I've a code:
...
struct device_defs {
unsigned int vendor;
unsigned int dev;
unsigned int mask;
unsigned long data;
};

static struct device_defs device_defs_tbl[] =
{
{ 0x0000, 0x0011, 0x0000, 0x0000, 0, 0, 0},
{ }
}

int main(void)
{
...
return 0;
}

What's correct way to initialize 'device_defs_tbl'?

static struct device_defs device_defs_tbl[] = {
{ 0x0000, 0x0011, 0x0000, 0x0000},
{ 0, 0, 0, 0}
};
 
R

Roman Mashak

Hello, pete!
You wrote on Thu, 04 Jan 2007 03:52:07 GMT:

??>> int main(void)
??>> {
??>> ...
??>> return 0;
??>> }
??>>
??>> What's correct way to initialize 'device_defs_tbl'?

p> static struct device_defs device_defs_tbl[] = {
p> { 0x0000, 0x0011, 0x0000, 0x0000},
p> { 0, 0, 0, 0}
p> };

I missed that. Thanks a lot. Seems like GNU extensions allow initializations
of only the first member of structure, as I've seen it in linux code base a
lot.

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
R

Richard Heathfield

Roman Mashak said:
Hello,

say I've a code:
...
struct device_defs {
unsigned int vendor;
unsigned int dev;
unsigned int mask;
unsigned long data;
};

static struct device_defs device_defs_tbl[] =
{
{ 0x0000, 0x0011, 0x0000, 0x0000, 0, 0, 0},
{ }
}

Yuk.

You could do this:

static struct device_defs device_defs_tbl[] =
{
{0, 0x11},
{0}
};

and take the hit on the warning. gcc is required by the C Standard to
initialise the table properly anyway for no fewer than two reasons
(firstly, it's static, and secondly, it's partly initialised).
What's correct way to initialize 'device_defs_tbl'? Running
'gcc -std=c99 -W -Wall' or '-ansi' both result in warnings:

Unfortunately, the C Standard allows implementations to spout any old
rubbish as long as they diagnose syntax errors and constraint violations
and as long as they correctly translate correct programs. What you've
discovered is that gcc is perfectly capable of producing silly and
misleading warning messages for perfectly correct code whose behaviour is
guaranteed by the Standard.
 
B

Barry Schwarz

Hello,

say I've a code:
...
struct device_defs {
unsigned int vendor;
unsigned int dev;
unsigned int mask;
unsigned long data;
};

You have a struct type with 4 members.
static struct device_defs device_defs_tbl[] =

You attempt to define an array of this struct type
{
{ 0x0000, 0x0011, 0x0000, 0x0000, 0, 0, 0},

For the first element of the array, you provide 7 initializers. Why?

You apparently want the array to have two elements. Is that correct.
}

int main(void)
{
...
return 0;
}

What's correct way to initialize 'device_defs_tbl'? Running
'gcc -std=c99 -W -Wall' or '-ansi' both result in warnings:

There are at least two options

static struct device_defs device_defs_tbl[2] =
{
{ 0x0000, 0x0011, 0x0000, 0x0000 }
}

or the possibly more flexible

static struct device_defs device_defs_tbl[] =
{
{ 0x0000, 0x0011, 0x0000, 0x0000 }
{ 0 }
}

You can remove the last two 0x0000 initializers in either case since
the standard requires the uninitialized portions of an initialized
aggregate object to be automatically initialized to 0, 0.0,or NULL as
appropriate.


Remove del for email
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top