Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Defining a union in ANSI/ISO C?

0 views
Skip to first unread message

Harry Potter

unread,
Aug 10, 2009, 11:30:39 AM8/10/09
to
Let's say I have a union declared. Then let's say I want to
initialize a variable of that type in a module and assign it a value.
An example follows:

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! :(

Alex Russell

unread,
Aug 10, 2009, 3:23:58 PM8/10/09
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

Harry Potter

unread,
Aug 11, 2009, 12:48:57 PM8/11/09
to
On Aug 10, 3:23 pm, Alex Russell <alexander.russ...@telus.net> wrote:
> xxx.a=3;
> xxx.b.a=3;  // we just over-wrote xxx.a
> strcpy(xxx.c, "fubar");  // and we over-wrote it again
>
So I can't do it. I resorted to assigning it a value using code to
this point. Thank you.
----------------

Alex Russell

unread,
Aug 11, 2009, 2:07:17 PM8/11/09
to

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

Harry Potter

unread,
Aug 12, 2009, 12:45:08 PM8/12/09
to
On Aug 11, 2:07 pm, Alex Russell <alexander.russ...@telus.net> wrote:
> 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

pe...@nospam.demon.co.uk

unread,
Aug 13, 2009, 12:54:06 AM8/13/09
to
In article <e0f0c9b3-dff2-4bd9...@l31g2000vbp.googlegroups.com>
maspet...@aol.com "Harry Potter" writes:

> 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."

Harry Potter

unread,
Aug 13, 2009, 12:45:19 PM8/13/09
to
On Aug 13, 12:54 am, p...@nospam.demon.co.uk wrote:
> In article  <e0f0c9b3-dff2-4bd9-8a5a-d1c57f220...@l31g2000vbp.googlegroups.com>
And I need to declare it as static. I pretty much already know which
routines are more efficient. In fact, the first incarnation of my
library used malloc(), but I replaced it with a pointer to an array in
memory.
-------------
Joseph Rose, a.k.a. Harry Potter
0 new messages