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

Unions in structures

I recently noticed (I'm new to the language) that when using a union
type as a component of a structure, you don't need a union-tag.

struct foo
{
char *ptr;
union
{
int i;
float f;
char c;
};
};

I have never read that a union type reference is optional in any
case.
Is this specific to the compiler, or part of the standard?
Mar 16 '08 #1
7 1509
blockstack wrote:
I recently noticed (I'm new to the language) that when using a union
type as a component of a structure, you don't need a union-tag.

struct foo
{
char *ptr;
union
{
int i;
float f;
char c;
};
};

I have never read that a union type reference is optional in any
case.
Is this specific to the compiler, or part of the standard?
A union does not need to have a union tag, and a struct
does not need to have a struct tag. But I think you may be
mixing up the terminology!

- A "struct tag" is an identifier that can be combined
with the keyword `struct' to form a name for a specific
struct type. In your example, `foo' is a struct tag
and `struct foo' is the name of the type. If the `foo'
were absent, the struct would have no tag and there
would be no way to write a name for its type elsewhere
in the program.

- Similarly, a "union tag" is an identifier that can be
combined with the keyword `union' to form a name for a
specific union type. In your example, the union has no
tag and there is no way to write the name of the union's
type. If the identifier `bar' appeared right after the
`union' keyword, `bar' would be the union tag and
`union bar' would be the name of the type.

- Almost every element contained in a struct or union (the
lone exception is not important here) must have an element
name. In your example, `i', `f', and `c' are the names
of the elements of the union. `ptr' is the name of one
element of the struct, but the other element (the union)
has no name. This is a compile-time error for which the
compiler is required to produce a diagnostic message.

So, why do you get no error message? Probably because you
are not using a C compiler, but a C-with-extras compiler that
accepts a C-like language that isn't really C. The gcc compiler
is like that in its default mode of operation; command-line flags
like `-ansi -pedantic' can make it conform more strictly to the
actual definition of C.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Mar 16 '08 #2
blockstack wrote:
>
I recently noticed (I'm new to the language) that when using a union
type as a component of a structure, you don't need a union-tag.

struct foo
{
char *ptr;
union
{
int i;
float f;
char c;
};
};

I have never read that a union type reference is optional in any
case.
Is this specific to the compiler, or part of the standard?
A footnote to Eric's response:

This convention produces headaches when/if struct foo is expanded
to:

struct foo
{ char *ptr;
unsigned i;
union
{ int i;
float f;
char c;
};
};

struct foo x;
x.i = 7; /* Which i in x? */

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 16 '08 #3
On Mar 16, 12:58 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
blockstack wrote:
I recently noticed (I'm new to the language) that when using a union
type as a component of a structure, you don't need a union-tag.
struct foo
{
char *ptr;
union
{
int i;
float f;
char c;
};
};
I have never read that a union type reference is optional in any
case.
Is this specific to the compiler, or part of the standard?

A union does not need to have a union tag, and a struct
does not need to have a struct tag. But I think you may be
mixing up the terminology!

- A "struct tag" is an identifier that can be combined
with the keyword `struct' to form a name for a specific
struct type. In your example, `foo' is a struct tag
and `struct foo' is the name of the type. If the `foo'
were absent, the struct would have no tag and there
would be no way to write a name for its type elsewhere
in the program.

- Similarly, a "union tag" is an identifier that can be
combined with the keyword `union' to form a name for a
specific union type. In your example, the union has no
tag and there is no way to write the name of the union's
type. If the identifier `bar' appeared right after the
`union' keyword, `bar' would be the union tag and
`union bar' would be the name of the type.

- Almost every element contained in a struct or union (the
lone exception is not important here) must have an element
name. In your example, `i', `f', and `c' are the names
of the elements of the union. `ptr' is the name of one
element of the struct, but the other element (the union)
has no name. This is a compile-time error for which the
compiler is required to produce a diagnostic message.

So, why do you get no error message? Probably because you
are not using a C compiler, but a C-with-extras compiler that
accepts a C-like language that isn't really C. The gcc compiler
is like that in its default mode of operation; command-line flags
like `-ansi -pedantic' can make it conform more strictly to the
actual definition of C.

--
Eric Sosman
esos...@ieee-dot-org.invalid
Thanks,
Using the option --ansi=pedantic did cause an error. I used union-tag
and union-type-reference synonymously as I thought both were
identifiers, but I see how I needed to make the distinction.
The reason I was asking was because I was reading an old K&R C book
from the 80s in which they were describing the previous code as a
union variation rather than a structure. And then I received no error,
so I wondered if this was part of the standard.
But it is not standard C. It does seem to make things easier though.
Mar 16 '08 #4
blockstack <bl********@gmail.comwrites:
I recently noticed (I'm new to the language) that when using a union
type as a component of a structure, you don't need a union-tag.

struct foo
{
char *ptr;
union
{
int i;
float f;
char c;
};
};

I have never read that a union type reference is optional in any
case.
Is this specific to the compiler, or part of the standard?
It's specific to the compiler. As far as the standard is concerned,
the above is an error requiring a diagnostic. Some compilers allow
anonymous unions as an extension.

In particular, gcc supports this. gcc's "-pedantic" option will cause
it to issue a warning.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 16 '08 #5
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
The uses I've seen, though, have
all been of a somewhat dubious nature, along the lines of

struct thing {
struct {
unsigned int readyFlag : 1;
unsigned int willingFlag : 1;
unsigned int ableFlag : 1;
}; /* no name */
unsigned int flags;
#define READY_FLAG 0x1
#define WILLING_FLAG 0x2
#define ABLE_FLAG 0x4
};
I think that "struct thing" should be "union thing".
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Mar 16 '08 #6
Ben Pfaff wrote:
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
>The uses I've seen, though, have
all been of a somewhat dubious nature, along the lines of

struct thing {
struct {
unsigned int readyFlag : 1;
unsigned int willingFlag : 1;
unsigned int ableFlag : 1;
}; /* no name */
unsigned int flags;
#define READY_FLAG 0x1
#define WILLING_FLAG 0x2
#define ABLE_FLAG 0x4
};

I think that "struct thing" should be "union thing".
Age has not withered nor custom staled the correctness
of your thought. Thanks.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Mar 16 '08 #7

"Eric Sosman" <es*****@ieee-dot-org.invalidschreef in bericht
news:2L******************************@comcast.com. ..
Age has not withered nor custom staled the correctness
of your thought. Thanks.
I somehow get the urge to print this out in big letters and hang it on the
wall. But eh what does it mean?

(literally)
Mar 17 '08 #8

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

Similar topics

11
by: Michael | last post by:
Hi guys I've been programming C++ for quite some time, but I have never used unions. I am trying to find a good exmaple of when they might be used. can anyone provide one?? Regards Michael.
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
9
by: Neil Zanella | last post by:
Hello, Some programmers like to use a coding convention where all names of variables that are pointers start with the letter p (and sometimes even use similar conventions for strings and other...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
2
by: minorguy | last post by:
1.) In C++/CLI, I have the following structures defined, which work as unions: public ref struct One { Int32 a; };
1
by: aarklon | last post by:
Hi folks, Recently i was reading the book C an advanced introduction by narain gehani, in page no:236 it is written as If a union contains several structures with a common initial sequence,...
3
by: Hallvard B Furuseth | last post by:
I'm getting horribly lost in the strict aliasing rules. Is this code correct? struct A { int x; }; struct B { int x, y; }; int foo( struct A *a ) { struct B *b = (struct B *) a; return b->x...
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
17
by: anon.asdf | last post by:
Hi! I want to assign the number 129 (binary 10000001) to the MSB (most significant byte) of a 4-byte long and leave the other lower bytes in- tact! -working on normal pentium (...endian) I...
6
by: Ravikiran | last post by:
Hi, I want know the differences between Unions and Structures in C programming. Thank you..
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.