473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Compiler warning on struct initialization...


Hello Group.

Please consider the following code:

/* this table is used in the wipedevice routine */
static const struct wipe_t
{
uchar wte[3]; /* wipe table entry */
} wipetable[] = {
{0x00, 0x00, 0x00},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0xFF, 0xFF, 0xFF},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0xAA, 0xAA, 0xAA},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0x55, 0x55, 0x55},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0x00, 0x00, 0x00}
};
When I compile the module, I'm getting the following error:

fs_dev.c:83: warning: missing braces around initializer
fs_dev.c:83: warning: (near initialization for `wipetable[0].wte')

uchar is defined as follows:

#define uchar unsigned char
This code appears before the table above, and there is no problem:

/* this table is used to help determine the CHS if the
device does not have or support CHS */
static const struct chs_xla_t
{
uint32 count;
uint16 cyl;
uint16 head;
uint16 sect;
uint16 block;
} chs_xla[] = {
{ 13668, 201, 4, 17, 68}, /* 7MB */
{ 20502, 201, 6, 17, 102}, /* 10MB */
{ 32436, 318, 6, 17, 102}, /* 16MB */
{ 41820, 615, 4, 17, 68}, /* 21MB */
{ 57222, 306, 11, 17, 187}, /* 28MB */
{ 62832, 462, 8, 17, 136}, /* 31MB */
{ 64000, 250, 16, 16, 256}, /* 32MB */
{ 80000, 400, 8, 25, 200}, /* 40MB */
{ 128000, 250, 16, 32, 512}, /* 64MB */
{ 256000, 250, 32, 32, 1024}, /* 128MB */
{ 512000, 250, 64, 32, 2048}, /* 256MB */
{ 1024000, 500, 64, 32, 2048}, /* 512MB */
{ 2048000, 500, 128, 32, 4096}, /* 1GB */
{ 4096000, 1000, 128, 32, 4096}, /* 2GB */
{ 8192000, 1024, 200, 40, 8000}, /* 4GB */
{16384000, 1024, 256, 63, 16128}, /* 8GB */
{16515072, 1024, 256, 63, 16128} /* Absolute Maximum for CHS */
};

Any ideas as to what the compiler is complaining about? I'm using gcc
on FreeBSD. The command line is as follows:

gcc -W -Wall -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes
-Wnested-externs -Wwrite-strings -Wfloat-equal -Winline -Wtrigraphs
-ansi -std=c89 -pedantic -ggdb3 -c -o fs_dev.o fs_dev.c
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fsck more fsck yes spray umount sleep
Jul 13 '07 #1
6 15023
On Jul 13, 2:59 pm, Daniel Rudy <spamt...@spamthis.netwrote:
Hello Group.

Please consider the following code:

/* this table is used in the wipedevice routine */
static const struct wipe_t
{
uchar wte[3]; /* wipe table entry */
} wipetable[] = {
{0x00, 0x00, 0x00},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0xFF, 0xFF, 0xFF},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0xAA, 0xAA, 0xAA},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0x55, 0x55, 0x55},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0x00, 0x00, 0x00}
};

When I compile the module, I'm getting the following error:

fs_dev.c:83: warning: missing braces around initializer
fs_dev.c:83: warning: (near initialization for `wipetable[0].wte')
An extra set of braces required in the above initialization.
Because the structure has an array member.

wipetable[] = {
{ {0x00, 0x00, 0x00} },
...
};

Mohan

Jul 13 '07 #2
On 13 Jul, 10:59, Daniel Rudy <spamt...@spamthis.netwrote:
Hello Group.

Please consider the following code:

/* this table is used in the wipedevice routine */
static const struct wipe_t
{
uchar wte[3]; /* wipe table entry */
} wipetable[] = {
{0x00, 0x00, 0x00},
....
};
This is a somewhat special case of a struct - with only one element.
>
When I compile the module, I'm getting the following error:

fs_dev.c:83: warning: missing braces around initializer
fs_dev.c:83: warning: (near initialization for `wipetable[0].wte')
In the line "{0x00, 0x00, 0x00}," the braces wrap the initialization
of the array.
In the context of the larger array of structs, each struct initializer
should also be braced.

When I used "{ {0x00, 0x00, 0x00} }," for the first line of the
initialization, I found that
the error moved on one line, confirming (or at least reinforcing) my
understanding of the issue.

[Over to the language lawyers to correct me]

Jul 13 '07 #3
On Fri, 13 Jul 2007 09:59:07 +0000, Daniel Rudy wrote:
[snip]
Any ideas as to what the compiler is complaining about? I'm using
gcc
on FreeBSD. The command line is as follows:

gcc -W -Wall -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes
-Wnested-externs -Wwrite-strings -Wfloat-equal -Winline -Wtrigraphs
-ansi -std=c89 -pedantic -ggdb3 -c -o fs_dev.o fs_dev.c
^^ ^^
Aren't those two synonymous?
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 13 '07 #4
At about the time of 7/13/2007 3:28 AM, ma**********@pobox.com stated
the following:
On 13 Jul, 10:59, Daniel Rudy <spamt...@spamthis.netwrote:
>Hello Group.

Please consider the following code:

/* this table is used in the wipedevice routine */
static const struct wipe_t
{
uchar wte[3]; /* wipe table entry */
} wipetable[] = {
{0x00, 0x00, 0x00},
...
> };

This is a somewhat special case of a struct - with only one element.
>When I compile the module, I'm getting the following error:

fs_dev.c:83: warning: missing braces around initializer
fs_dev.c:83: warning: (near initialization for `wipetable[0].wte')

In the line "{0x00, 0x00, 0x00}," the braces wrap the initialization
of the array.
In the context of the larger array of structs, each struct initializer
should also be braced.
That explains it, and corrects it. Thanks.
When I used "{ {0x00, 0x00, 0x00} }," for the first line of the
initialization, I found that
the error moved on one line, confirming (or at least reinforcing) my
understanding of the issue.

[Over to the language lawyers to correct me]

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fsck more fsck yes spray umount sleep
Jul 13 '07 #5
In article <L1*****************@newssvr11.news.prodigy.net> ,
Daniel Rudy <ZGNydWR5QHBhY2JlbGwubmV0wrote:

[some rearrangement and vertical compression applied]
>#define uchar unsigned char
static const struct wipe_t {
uchar wte[3]; /* wipe table entry */
} wipetable[] = {
So, "wipetable" has type "array ? of struct ...", where the
question-mark (representing an unknown-as-yet size) will be
filled in by the number of elements initialized.

The struct has type "struct wipe_t", and contains one member,
"wte". This memeber has type "array 3 of unsigned char".

The final opening brace above begins the initializer for the
outermost array, i.e., "wipetable" itself.
{0x00, 0x00, 0x00},
This line has an open brace, a list of values, and a close brace.

The open brace can be applied to the "next available aggregate".
Aggregate types include both arrays and structures, and the next
available one is "struct wipe_t".

That uses up the open brace, so that we just have the three values.
The thing to be initialized at this point is an array, which is
an aggregate, and logically "ought to" have an open brace.
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0xFF, 0xFF, 0xFF},
{0x92, 0x29, 0x24},
[etc]

These go on to initialize wipetable[1], wipetable[2], and so on;
all of them have the same set of "missing" braces.
>When I compile the module, I'm getting the following error:

fs_dev.c:83: warning: missing braces around initializer
fs_dev.c:83: warning: (near initialization for `wipetable[0].wte')
which is what gcc produces when you have a partially-bracketed
initializer (as in this case), and you ask for the warning about
partially-bracketed initializers ("-Wmissing-braces", in at least
some versions, and implied by "-Wall").
>This code appears before the table above, and there is no problem:

/* this table is used to help determine the CHS if the
device does not have or support CHS */
static const struct chs_xla_t
{
uint32 count;
uint16 cyl;
uint16 head;
uint16 sect;
uint16 block;
} chs_xla[] = {
Here chs_xla has type "array ? of struct chs_xla_t". As before,
the question-mark will be filled in by the initializer. The struct
type has five elements, each an ordinary integer (assuming "the
usual" typedefs for uint32 and uint16 anyway).
{ 13668, 201, 4, 17, 68}, /* 7MB */
The open brace indicates the next available aggregate, i.e.,
the struct making up chs_xla[0]. Now we are down to the list
of values. The next "thing to be initialized" is an ordinary
integer, which does not require a brace, so chs_xla[0].count
is to be set to 13668. This leaves the 201 for chs_xla[0].cyl,
and so forth.

If any members of "struct chs_xla_t" had been aggregates themselves,
you would need more braces (at least, to remove the warning). For
instance:

struct foo {
int a;
int b[2];
int c;
} foo[] = {
{ 1, { 2, 3 }, 4 },
{ 9, { 8, 7 }, 6 },
};

would be fully-bracketed.

Nothing requires initializers in C to be fully-bracketed, although
it is probably usually a good idea anyway.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 13 '07 #6
Mohan <aj****@gmail.comwrites:
On Jul 13, 2:59 pm, Daniel Rudy <spamt...@spamthis.netwrote:
>Please consider the following code:

/* this table is used in the wipedevice routine */
static const struct wipe_t
{
uchar wte[3]; /* wipe table entry */
} wipetable[] = {
{0x00, 0x00, 0x00},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0xFF, 0xFF, 0xFF},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0xAA, 0xAA, 0xAA},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0x55, 0x55, 0x55},
{0x92, 0x29, 0x24},
{0x6D, 0xB6, 0xDB},
{0x00, 0x00, 0x00}
};

When I compile the module, I'm getting the following error:

fs_dev.c:83: warning: missing braces around initializer
fs_dev.c:83: warning: (near initialization for `wipetable[0].wte')

An extra set of braces required in the above initialization.
Because the structure has an array member.

wipetable[] = {
{ {0x00, 0x00, 0x00} },
...
};
The extra braces are a good idea, but they're not actually required.
The language rules about braces in initializers are fairly lax. For
example, this:

int arr[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 };

is legal, even though this:

int arr[2][2][2] = { { {1, 2}, {3, 4} },
{ {5, 6}, {7, 8} } };

is clearer. The compiler is merely warning about the "missing" braces
(appropriately, IMHO); there's no constraint violation or syntax
error.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 13 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert A Riedel | last post by:
I have a class that is intended to be exported in a DLL that uses another class that is in a static library. All clients that use the DLL will also link with the same static library. In summary,...
5
by: Alan Cobb | last post by:
Hi, In the managed C++ class below I get compile warning C4677 from VS2003. "signature of non-private function contains assembly private type", even though the managed enum is public. I have...
4
by: Everton | last post by:
The task at hand is to initialize a variable of type "struct in6_addr" in a portable way. For instance: /* NetBSD - /usr/include/netinet6/in6.h */ struct in6_addr { union { __uint8_t ...
3
by: jilerner | last post by:
Question about C99/gcc struct initialization: void ffoo(void) { struct FOO { int a,b,c; }; struct foo = { .b = 22 }; What happens now to foo.a and foo.c ? Are they initialized to 0, or left...
1
by: John Harris | last post by:
We have some C++ code that has a makefile which contains both /W2 and /W3. Due to the way the makefiles are written to be shared across multiple projects, it's not trival to eliminate the duplicate...
4
by: silversurfer2025 | last post by:
Hello everyone, I am using a struct inside another class, which can be used through an empty constructor or another one. The definition looks like this: struct Entry{ int index; //line 38...
11
by: zeppe | last post by:
Hi all, I've a problem. The code that follows creates a warning in both gcc and visual c++. However, I think it's correct: basically, there is a function that return an object of a derived...
6
by: James H. Newman | last post by:
I am playing with some code that has been automatically generated from ASN.1 data specification found in RFC 3280. One of the structures generated reads as follows: typedef struct TBSCertList {...
9
by: bacbacdinner | last post by:
Can anyone explain to me why the code below generates a warning? ///////////////////////////////////////////////////////////// typedef struct testStruct * TestStructRef; typedef TestStructRef * ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.