Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > union in struct without union name

Reply
Thread Tools

union in struct without union name

 
 
Peter Dunker
Guest
Posts: n/a
 
      04-26-2004
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


 
Reply With Quote
 
 
 
 
xarax
Guest
Posts: n/a
 
      04-26-2004
"Peter Dunker" <(E-Mail Removed)-ilmenau.de> wrote in message
news:c6jgen$8a2$(E-Mail Removed)-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!


 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      04-26-2004
>"Peter Dunker" <(E-Mail Removed)-ilmenau.de> wrote in message
>news:c6jgen$8a2$(E-Mail Removed)-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:kOcjc.13591$(E-Mail Removed) link.net>
xarax <(E-Mail Removed)> 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 . 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 (4039.22'N, 11150.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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
struct or class object with constructor in UNION flags as error. !! iceColdFire C++ 4 05-06-2005 10:18 AM
Union with anonymous struct JKop C++ 5 10-26-2004 03:54 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments