Struct declaration with no tag and no init-declarator-list

J

JoseMariaSola

The following declaration is valid:

struct {int x;};

There's no tag and no variable.

Does it has any use?

Thanks.

Joey.
 
V

viza

Hi

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;

HTH

viza
 
V

viza

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:

PS: I didn't realise that that's non-standard. Flames may follow...
 
H

Harald van Dijk

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.
 
K

Keith Thompson

viza said:
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).
 
J

JoseMariaSola

The following declaration is valid:
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? My compiler doesn't detect it. Is it
broken?
In the Standard I didn't find any restriction.
 
H

Harald van Dijk

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.
 
J

JoseMariaSola

It's a constraint violation, which requires a diagnostic from any
conforming implementation.


If you asked it to try to conform to the standard, then yes.


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.

Thanks!
 

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
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top