struct _z
{
unsigned a;
signed b;
char * c;
};
union t1
{
int a;
struct _z b;
char c[32];
};
union t1 xxx=
{
????
};
How do I define, let's say, for which element in union t1 I am
defining for xxx?
-------------
Joseph Rose, a.k.a. Harry Potter
Working magic in the computer community...or at least striving to! :(
xxx.a=3;
xxx.b.a=3; // we just over-wrote xxx.a
strcpy(xxx.c, "fubar"); // and we over-wrote it again
Everything declared in the union starts at the same place in memory. You
use the usual "." syntax to pick which of the union members you are
dealing with. The size of a union will be the size of its largest member.
Alex R
What are you trying to do with the union?
Generally, unions are used when you want to pass the same type of thing
to many different, but related, functions, and need slightly different
data types depending on the situation. For example, a gui lib may use
unions for all the various widgets and have the first part of all unions
the same, and have the last part differ depending on the widget. All
widgets have a width, height, id, etc... but some have more information
than others.
Alex R
I am working on a simple UI library in C for 8-bit computers. The
control information data contains a union that describes things like
where to store the data and, if a selection control (like an over-
simplified combo box), the possible values. Until now, I have defined
the rest of the UI dialog-box data declared as static and have code
write the data-holders. If I can declare the latter as static, I can
save perhaps up to 50-120 bytes of code.
-----------
Joseph Rose, a,k,a, Harry Potter
> I am working on a simple UI library in C for 8-bit computers.
Ah -- BTDT. Coaxing elegant C into 48K (or whatever TPA you have
available) is no mean feat. I assume you're developing with a
cross-compiler? If not, you will find that switching to one will
speed your development considerably. You might find more help in
comp.os.cpm or in one of the comp.arch or comp.programming groups.
> The
> control information data contains a union that describes things like
> where to store the data and, if a selection control (like an over-
> simplified combo box), the possible values. Until now, I have defined
> the rest of the UI dialog-box data declared as static and have code
> write the data-holders. If I can declare the latter as static, I can
> save perhaps up to 50-120 bytes of code.
Perhaps you could try a struct that contains a union member?
Maybe something like
struct cid {
enum cid_type;
union cid_data {
short a;
long b;
char s[BUFLEN];
etc...
}
}
where cid_type is set to the "type" of data in the union? cid_type
can also hold a value UNUSED, signifying that the (static) struct
is "available for use". In my experience, the overhead of using
malloc() and friends from the C lib is far more expensive than
declaring static but reusable data structs.
Pete
--
"We have not inherited the earth from our ancestors,
we have borrowed it from our descendants."