Re: missing braces around initializer



Twirlip of the Mists <twirlip@xxxxxxxxxxx> writes:
On Wed, 13 May 2009 08:44:24 -0700, Keith Thompson wrote:
[...]
The language allows you to omit the inner braces, but why would you want
to?

There is one fairly common case, auto structs and arrays that you want
initialized to 0, 0.0 or NULL as appropriate. Consider this example:

struct fred {
int x;
float y[3];
char *message[2];
} barney[3][4][7] = {0};

"{0}" is a universal initializer that works for anything (I believe). I
would like compilers to treat it as a special exception if they are going
to warn about missing braces or struct members in initializers. I found
gcc could be worse: "{{{{0}}}}" will silence the warning. At least you
don't need to initialize all the members.

Agreed -- and yes, it's annoying that gcc warns about this.

But for anything other than {0}, I prefer to use fully braced
initializers.

(And yes, hexapodia *is* the key insight.)

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: struct versioning
    ... to be initialized by client code, by taking the sizeof of the struct. ... write the WIDGET macro-opcode and provide operands for the ... was very often a length indicator for the whole block. ... C's initializers, even now, are not robust ...
    (comp.lang.c)
  • Re: [RFC 04/10] Temporary struct vfs_lookup in file_permission
    ... Used to check for read/write/execute permissions on an already opened ... int file_permission(struct file *file, int mask) ... I didn't use initializers because they initialize the entire data structure. ... In case of struct vfs_lookup, unless the LOOKUP_OPEN flag is set, then the ...
    (Linux-Kernel)
  • Re: [PATCH] Detect mmconfig on nVidia MCP55
    ... more reviewable - and more pleasant to ... Same goes for static structure initializers. ... static const struct file_operations perf_fops = { ... The typos are of the ...
    (Linux-Kernel)
  • Re: [patch] add private data to notifier_block
    ... notifier_block struct to add a void *, ... initializers -- their bare priority initialization might now shift to ... putting the void * after the int. ...
    (Linux-Kernel)
  • Re: [RFC PATCH] cpumask_t initializers
    ... >> initializers has curly braces on the outside of the list. ... GCC doesn't ... >> seem to mind if there are parens outside the braces for a struct ... >> version of GCC's C parser. ...
    (Linux-Kernel)