unions within an array

K

karthikbalaguru

Hi,
I came across statements that claim that
Unions may occur within arrays.
But, a union is a variable that may hold (at different times)
objects of different types and sizes.
whereas in an array, each element should have the same
data type . So, how can an array hold a union that has different
ojects of different types during different scenarios ?

Thx in advans,
Karthik Balaguru
 
J

Jens Thoms Toerring

karthikbalaguru said:
I came across statements that claim that
Unions may occur within arrays.
But, a union is a variable that may hold (at different times)
objects of different types and sizes.
whereas in an array, each element should have the same
data type . So, how can an array hold a union that has different
ojects of different types during different scenarios ?

Perhaps I am misunderstanding your question, but why shouldn't
you have an array of unions? Then each element of the array has
the same type, i.e. type union. E.g.

union {
int a;
double b;
const char * c;
} x[ 3 ];

x[0].a = 3;
x[1].b = 1.23456;
x[2].c = "Hello world";

etc.
Regards, Jens
 
J

jameskuyper

karthikbalaguru said:
Hi,
I came across statements that claim that
Unions may occur within arrays.
But, a union is a variable that may hold (at different times)
objects of different types and sizes.
whereas in an array, each element should have the same
data type . So, how can an array hold a union that has different
ojects of different types during different scenarios ?

There's no conflict between those two facts. The types and sizes of
the members of the union can be different - that's the whole point of
having one. At any given time, you can only safely read the value of
the same member of the union that was last written to (with certain
exceptions that don't matter here). However, the type and size of the
union object itself does not change, no matter which member is
currently active, so it's perfectly legal to create an array of union
objects.
 
M

Martin Ambuhl

karthikbalaguru said:
... in an array, each element should have the same
data type . So, how can an array hold a union that has different
ojects of different types during different scenarios ?

Given

#define NUS 47
union U { double d; char *x; };
union U foo[NUS];

each of the foo has the same type; namely, union U.
 
K

Keith Thompson

Kenneth Brody said:
The array elements _are_ all the same. They're all instances of that
union. The fact that the union can contain different things is
irrelevent.

Consider a fruit basket, which can contain any type of fruit. Then,
line up a bunch of them as an array. Each element of this array is
the same -- a fruit basket. However, the first may have an apple, the
second an orange, and so on.

Right. But the analogy breaks down a little because you can't tell
what kind of fruit a basket contains by looking into it. You have to
keep track of that information separately.
 
J

jfbode1029

Hi,
I came across statements that claim that
Unions may occur within arrays.
But, a union is a variable that may hold (at different times)
objects of different types and sizes.
whereas in an array, each element should have the same
data type . So, how can an array hold a union that has different
ojects of different types during different scenarios ?

Thx in advans,
Karthik Balaguru

First of all, don't confuse the union type with the types of its
members. Given a declaration like

union foo {char x; int y; long double z;} uArr[10];

each element of uArr is of the same type, which is of type "union
foo".

Secondly, the size of a union type is (at least) the size of its
largest member. In the example above, each element of uArr is (at
least) the same size as a long double, because that's the largest type
in the union. The size of the union stays the same regardless of
which member you last assigned.
 
L

luser-ex-troll

Well, since I didn't label the basket as to which type of fruit it contains,
examining the contents invokes UB.  Fortunately, this basket was made by the
Schrödinger Fruit Basket Company.

Does that mean there's a half-dead cat inside the apple?

;{>

lxt
 
C

CBFalconer

luser-ex-troll said:
.... snip ...


Does that mean there's a half-dead cat inside the apple?

Schrodingers kitty was never half dead. Its condition is unknown,
until positively exposed.
 
K

Keith Thompson

Kenneth Brody said:
(e-mail address removed) wrote:
union foo {char x; int y; long double z;} uArr[10];
In the example above, each element of uArr is (at least) the same size
as a long double, because that's the largest type in the union.
What if sizeof(int) > sizeof(long double)?

Sorry. I should have included a statement along the lines of
"assuming sizeof(char) < sizeof(int) < sizeof(long double)".

I believe "sizeof(char) <= sizeof(int)" is guaranteed.

Yes, but sizeof(char) < sizeof(int) is not (sizeof(int) can be 1 if
CHAR_BIT is at least 16). But I think (though I'm not sure) that the
guaranteed condition sizeof(char) <= sizeof(int) was sufficient for
the example.
I'm not so
sure about the truth of "sizeof(int) <= sizeof(long double)", though I
wouldn't be surprised if it is guaranteed as well.

It isn't, though I've never heard of an implementation where it wasn't
true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top