Re: storing related structs
- From: "Army1987" <please.ask@xxxxxx>
- Date: Sat, 30 Jun 2007 14:15:45 +0200
"Gray Alien" <grey@xxxxxxxxxxxxx> ha scritto nel messaggio news:Z7OdnWRcAeZVoxvbnZ2dnUVZ8vidnZ2d@xxxxxxxxx
I have two related structs:struct C {
struct A
{
int x ;
void * data ;
};
and
struct B
{
int x, y ;
double z ;
void * data1, *data2 ;
};
These structures (not pointers to them), need to be stored in a another struct:
struct C
{
unsigned id ;
char name ;
DATA_TYPE the_struct ;
};
Where DATA_TYPE is EITHER a struct A or struct B.
For compatability with existing libraries, I cannot use a void * (for instance), and cast between struct A and B.
Is there a soln to this problem ?
unsigned id;
char name; /* do you mean *name, or name[LARGE_ENOUGH]? */
enum { strA, strB } type;
union {
struct A str_A;
struct B str_B;
} the_struct;
};
A union is like a struct, but can only hold an element at a time.
You access members of unions like members of struct. For example,
in the case above to access the double of a struct B in a struct C,
you'll have to use mystructC.the_struct.str_B.z
Or try to find a better way to do what you're trying to do.
.
- Follow-Ups:
- Re: storing related structs
- From: Grey Alien
- Re: storing related structs
- From: Grey Alien
- Re: storing related structs
- From: gw7rib
- Re: storing related structs
- References:
- storing related structs
- From: Gray Alien
- storing related structs
- Prev by Date: [OT] Re: Display C in HTML
- Next by Date: Re: storing related structs
- Previous by thread: storing related structs
- Next by thread: Re: storing related structs
- Index(es):
Relevant Pages
|