Re: storing related structs



gw7rib@xxxxxxx wrote, On 30/06/07 13:25:
On 30 Jun, 13:15, "Army1987" <please....@xxxxxx> wrote:
"Gray Alien" <g...@xxxxxxxxxxxxx> ha scritto nel messaggionews:Z7OdnWRcAeZVoxvbnZ2dnUVZ8vidnZ2d@xxxxxxxxx

<snip>

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 ?
struct C {
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.

To the OP: One way of doing it would be to use a union, as Army1987
has suggested. However, if space isn't an issue, you might do better
including both a struct A and a struct B, and just using the one that
you want. This would seem to have less chance of going horribly wrong,
and might make your code clearer. Is there some reason why this would
not work?

Hope that helps.

If it is only valid to have one or the other then a union expresses the intent better and if you are accessing the wrong member something has already gone horribly wrong.
--
Flash Gordon
.



Relevant Pages

  • Re: Global (non _KERNEL) place for sockaddr_union?
    ... The problem is that the definition of the union depends on what you wish ... and which address families are visible to the application ... You may find that such a definition has to have conditionalized members. ... type 'struct sockaddr_storage' if it's to support all address families ...
    (freebsd-arch)
  • Re: Global (non _KERNEL) place for sockaddr_union?
    ... The problem is that the definition of the union depends on what you wish ... and which address families are visible to the application ... You may find that such a definition has to have conditionalized members. ... type 'struct sockaddr_storage' if it's to support all address families ...
    (freebsd-hackers)
  • Re: Global (non _KERNEL) place for sockaddr_union?
    ... The problem is that the definition of the union depends on what you wish ... and which address families are visible to the application ... You may find that such a definition has to have conditionalized members. ... type 'struct sockaddr_storage' if it's to support all address families ...
    (freebsd-net)
  • Re: "common initial sequence" rule = non-obvious constraints on padding?
    ... of any member struct of a union which currently contains such a struct ... anywhere that union type is visible. ... I'm sure that on the DS9000, the linker searches the complete program for unions and adds random padding between structure members in a way that breaks this assumption. ... compatible, you can declare the same object twice, using both types, without breaking the program. ...
    (comp.std.c)
  • [RFC: 2.6 patch] drivers/acpi/: possible cleanups
    ... +static void __exit ... union acpi_operand_object ... struct acpi_namespace_node ...
    (Linux-Kernel)