Re: Struct declaration with no tag and no init-declarator-list
viza <tom.viza@gmil. comwrites:
On Thu, 12 Jun 2008 08:19:58 -0700, JoseMariaSola wrote:
>
>struct {int x;};
>>
>There's no tag and no variable.
>>
>Does it has any use?
>
On it's own, it does nothing. Inside another struct or union, its
member(s) can be accessed as if they were members of the parent. eg:
>
struct {
struct {int x;};
}
foo;
>
foo. x= 7;
>
which is easier than:
>
struct {
struct {int x;} bar;
}
foo;
>
foo. bar. x= 7;
No, you can't, at least not in standard C. Some compilers may offer
it as an extension. But if you want your code to be portable, you'll
just have to write "foo.bar.x" (or do some ugly macro stuff).
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <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"
Re: Struct declaration with no tag and no init-declarator-list
On Thu, 12 Jun 2008 09:13:27 -0700, JoseMariaSola wrote:
The following declaration is valid:
>>
struct {int x;};
>>
>No, it isn't. It's not a syntax error, but C requires a declaration to
>actually declare something.
>
Is it a static semantic error?
It's a constraint violation, which requires a diagnostic from any
conforming implementation.
My compiler doesn't detect it. Is it
broken?
If you asked it to try to conform to the standard, then yes.
In the Standard I didn't find any restriction.
It's in 6.7p2: "A declaration shall declare at least a declarator (other
than the parameters of a function or the members of a structure or union),
a tag, or the members of an enumeration." Your declaration does not
declare any of those.
On Thu, 12 Jun 2008 09:13:27 -0700, JoseMariaSola wrote:
The following declaration is valid:
>
struct {int x;};
>
No, it isn't. It's not a syntax error, but C requires a declaration to
actually declare something.
>
Is it a static semantic error?
>
It's a constraint violation, which requires a diagnostic from any
conforming implementation.
>
My compiler doesn't detect it. Is it
broken?
>
If you asked it to try to conform to the standard, then yes.
>
In the Standard I didn't find any restriction.
>
It's in 6.7p2: "A declaration shall declare at least a declarator (other
than the parameters of a function or the members of a structure or union),
a tag, or the members of an enumeration." Your declaration does not
declare any of those.
Comment