473,395 Members | 1,458 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,395 software developers and data experts.

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

The following declaration is valid:

struct {int x;};

There's no tag and no variable.

Does it has any use?

Thanks.

Joey.
Jun 27 '08 #1
7 2154
Hi

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;

HTH

viza
Jun 27 '08 #2
On Thu, 12 Jun 2008 15:25:36 +0000, viza wrote:
>struct {int x;};

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...
Jun 27 '08 #3
On Thu, 12 Jun 2008 08:19:58 -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.
Jun 27 '08 #4
viza <to******@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_Keith) ks***@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"
Jun 27 '08 #5
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? My compiler doesn't detect it. Is it
broken?
In the Standard I didn't find any restriction.
Jun 27 '08 #6
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.
Jun 27 '08 #7
On Jun 12, 1:16 pm, Harald van D©¦k <true...@gmail.comwrote:
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.
Thanks!
Jun 27 '08 #8

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

Similar topics

1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
3
by: S Austin | last post by:
I would like to define a structure (a file header, in this case) that includes certain constants, and ensure that those members of the structure are always initialized to the same value. ...
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
6
by: sathyashrayan | last post by:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct tree { int data; struct tree *left,*right; }; void init(struct tree *node)
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
3
by: c8prog | last post by:
I want to save integer value in array(int key) of struct array (Element key; public int link; Element(int init) { init = 0; key = new int; link = init; } };
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
14
by: Carramba | last post by:
Hi! I have a big struct and I want to initialize it at once, but I get parse error before "{" compiler error. can't it by done? #include <stdio.h> #include <stdlib.h> typedef struct{ int b;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.