473,503 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

union in struct without union name

Hi,

I will write ANSI C89.
Is the following struct defenition correct ?
I wrote it with VC6(Windows IDE) and at first no Problem.
As I changed a compiler switch to 'no language extension',
the compiler said that the union has no name.

Is it right that in ANSI C the union must be named inside this kind of
structure ?
struct _S_ITEM
{
char *name;
int nb_of_bits;
int type;
PS_ITEM len_ref;
PS_DESCR hlp_des;

union {
struct{
void* p_value;
void* p_default;
}value;

struct{
char* name;
void* child;
}group;

struct{
PS_ITEM number_ref;
char* name;
char* id;
void* child;
}loop;
};
};
Thx Peter
Nov 14 '05 #1
2 5037
"Peter Dunker" <Pe**********@stud.tu-ilmenau.de> wrote in message
news:c6**********@piggy.rz.tu-ilmenau.de...
Hi,

I will write ANSI C89.
Is the following struct defenition correct ?
I wrote it with VC6(Windows IDE) and at first no Problem.
As I changed a compiler switch to 'no language extension',
the compiler said that the union has no name.

Is it right that in ANSI C the union must be named inside this kind of
structure ?
struct _S_ITEM
{
char *name;
int nb_of_bits;
int type;
PS_ITEM len_ref;
PS_DESCR hlp_des;

union {
struct{
void* p_value;
void* p_default;
}value;

struct{
char* name;
void* child;
}group;

struct{
PS_ITEM number_ref;
char* name;
char* id;
void* child;
}loop;
};
};
Thx Peter


AFAIK, M$ VC6 gets this wrong. It complains about
unnamed struct/union definitions. Your proposed
definition appears to be valid C, except for the
leading underscore in the outer tag name.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!
Nov 14 '05 #2
>"Peter Dunker" <Pe**********@stud.tu-ilmenau.de> wrote in message
news:c6**********@piggy.rz.tu-ilmenau.de...
... Is it right that in ANSI C the union must be named inside this
kind of structure ?

[much snippage]

The example can be simplified to, e.g.:

struct S {
union {
int i;
char *cp;
};
};

In article <news:kO*****************@newsread2.news.pas.earth link.net>
xarax <xa***@email.com> writes:AFAIK, M$ VC6 gets this wrong.


I believe it is correct. Note that this can be confusing, though,
as there are *two* names omitted: the union itself has no tag --
this is the first (and unimportant) omission -- *and* the structure
member has no name. It is this second omission that causes the
problem. To see why, replace the above with:

union U { int i; char *cp; };

struct S { union U /*nothing*/; };

Note that "struct S" is now empty!

C++, Plan 9 C, and GNU C -- all three are different languages from
C and all three are different from each other -- all allow this,
however, with similar semantics. It might be nice if C99 had
adopted part of th meaning provided in these three other languages.
In those three languages, if you are defining a struct or union
such as "struct S", and you mention the name of another struct
or union without giving a field-name:

struct S { /* begin defining a new type named S */
struct T; /* import T's members */
union U; /* import U's members */
char *last; /* add a member of our own */
}; /* finish up the new type named "S" */

If the types named T and U here were previously defined as, e.g.:

struct T { double zorg; };
union U { int i; char *cp; };

then the type named S would now countain four members named "zorg",
"i", "cp", and "last", with "zorg" at offset 0 and "i" and "cp"
sharing a field in the usual manner of a union, most likley at
offset sizeof(double) (probably 8). The member "last" would be
last, most likely at offset 12 or 16 or so.

Plan 9 C goes rather further than GNU C: an "imported" sub-structure
(or sub-union, but unions are effectively just the degenerate case
of structures) adds a special "type-name-member", as it were, to
the outer structure. If an S contains a T and U as above, for
instance, and you have an actual S and functions that require a
T or a U (or pointer thereto):

struct S object;

extern void takes_a_T(struct T);
extern void takes_a_Ustar(union U *);

you can pass the object itself (or its address) to the function,
and the compiler automatically locates the embedded sub-object:

takes_a_T(object); /* passes the T part of the S */
takes_a_Ustar(&object); /* passes pointer to the U part of the S */

This gives access to one of the most useful parts of C++ without
carrying all the baggage that C++ has to lug around.
--
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.
Nov 14 '05 #3

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

Similar topics

5
8126
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
67
10654
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
10
11361
by: tapeesh | last post by:
I created a C file say struct.c with the following structure declarations in the same file struct A { union key { int i; float f; }k1;
5
1685
by: Mike | last post by:
Within the following structure, TopStruct, I'd like to create 3 other structures, 2 of which make up a union. The first structure will always contain some data that I need and should never be...
8
1667
by: Mike | last post by:
The following struct, DataStruct, is only part of a larger one that contains additional fields and arrays. I need the explicit layout because this struct is really a union, where some of the...
50
6415
by: Mikhail Teterin | last post by:
Hello! The sample program below is compiled fine by gcc (with -Wall), but rejected by Sun's SUNWspro compiler (version 6 update 2). The point of contention is, whether a value for one of the...
3
8772
by: Lighter | last post by:
Why does Platform SDK define union LARGE_INTEGER in such a way? ntdef.h defines the union LARGE_INTEGER as follows: typedef union _LARGE_INTEGER { struct { ULONG LowPart;
18
6059
by: Bryan Parkoff | last post by:
I hate using struct / union with dot between two words. How can I use one word instead of two words because I want the source code look reading clear. three variables are shared inside one...
14
28076
by: deepak | last post by:
Hi Experts, I'm getting this compilation error while trying to access a member in structure. at what time we will get this error message? Thanks, Deepak
0
7205
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
7353
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...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.