Reputation: 21955
In the below piece of code :
union
{
float dollars;
int yens;
}price;
Is price an instance of an anonymous union or is it the name of the union itself. It looks to me like an instance but the author of the code says it's the name of the union.
Now, the second question - Suppose if this union is embedded in a structure like below:
struct
{
char title[50];
char author[50];
union
{
float dollars;
int yens;
}price;
}book1;
Would an access book1.dollars be valid?
Upvotes: 0
Views: 150
Reputation: 1407
1) It is an instance of an anonymous union indeed, the author was wrong. If you prepend the union with a typedef
keyword like
typedef union
{
float dollars;
int yens;
} price;
, then price
will be a typename referring to the union.
2) Nope, book1.price.dollars
you want to use instead.
UPDATE: An union type can be declared in the following way either:
union price
{
float dollars;
int yens;
}; // here you see, there is no instance.
If there was an identifier between the closing brace and the semicolon, that would be the name of the instance.
Upvotes: 2