Re: union of pointers to structs

Discussion in 'C Programming' started by Tim Rentsch, Jan 13, 2010.

  1. Tim Rentsch

    Tim Rentsch Guest

    "Paul" <-> writes:

    > Please could you offer some advice as to whether
    > the following code is good or bad practice.
    >
    > It seems a nice versatile way to create arrays of
    > 'Item' that point to different types of 'Object'.
    >
    > It is the most convenient way I could come up with
    > to use one array to access many different structures.
    >
    > n.b. perhaps the names 'Item' and 'Object' are
    > innapropriate for this use. I'm not sure.
    >
    > Thanks,
    > Paul.
    >
    > typedef struct
    > {
    > int x;
    > }
    > StructTypeA ;
    >
    > typedef struct
    > {
    > int y;
    > }
    > StructTypeB;
    >
    > typedef struct
    > {
    > int z;
    > }
    > StructTypeC;
    >
    > typedef union
    > {
    > StructTypeA *StructA;
    > StructTypeB *StructB;
    > StructTypeC *StructC;
    > }
    > GenericPointer;
    >
    > typedef struct
    > {
    > int Type;
    > GenericPointer Object;
    > }
    > Item;
    >
    > enum
    > {
    > TypeA,
    > TypeB,
    > TypeC
    > };
    >
    >
    > void ProcessItem (Item SomeItem)
    > {
    > if (SomeItem.Type == TypeA)
    > {
    > SomeItem.Object.StructA->x++;
    > }
    > else if (SomeItem.Type == TypeB)
    > {
    > SomeItem.Object.StructB->y++;
    > }
    > else if (SomeItem.Type == TypeC)
    > {
    > SomeItem.Object.StructC->z++;
    > }
    > }


    As far as the C language goes, there is nothing wrong
    with it. In terms of program design, it might or
    might not be a good choice, depending on what the
    other constraints and requirements are. It's hard
    to say whether something is a good solution without
    knowing first what is the problem. It might be helpful
    to say more about the problem you're trying to solve,
    including the context for the program, and also outline
    what other approaches you've tried or considered.
    What is important in terms of constructing a solution?
    How much do you care about type safety? About not
    having to write casts? Are the structures in questions
    ones you get to define, or are they defined in code
    over which you have no control? I'm sure there are
    other points you might want to bring up but those should
    get you started.
     
    Tim Rentsch, Jan 13, 2010
    #1
    1. Advertising

  2. Tim Rentsch

    Tim Rentsch Guest

    "Paul" <-> writes:

    >> As far as the C language goes, there is nothing wrong
    >> with it. In terms of program design, it might or
    >> might not be a good choice, depending on what the
    >> other constraints and requirements are. It's hard
    >> to say whether something is a good solution without
    >> knowing first what is the problem. It might be helpful
    >> to say more about the problem you're trying to solve,
    >> including the context for the program, and also outline
    >> what other approaches you've tried or considered.
    >> What is important in terms of constructing a solution?
    >> How much do you care about type safety? About not
    >> having to write casts? Are the structures in questions
    >> ones you get to define, or are they defined in code
    >> over which you have no control? I'm sure there are
    >> other points you might want to bring up but those should
    >> get you started.

    >
    > Okay, I've changed to void* (I preferred the specificness
    > of a union, but disliked the pre C99 inability to declare
    > a const one with the right fields filled in.
    >
    > The purpous is to display screens on an embedded
    > system, some of which are menus. There is some
    > updated _rough_ code below.
    >
    > My question is less relevant now since I quite like the
    > code, and it is seeming safe, so please only let me
    > know if you think it is a terrible approach, or likely
    > to cause problems or bad behaviour :)
    >
    > [code snipped]


    There are no problems with portability or undefined
    behavior as far as I can see. (That's not to say
    there aren't any, only that I didn't see any if
    there were.)

    If it were me writing the program I would probably
    make some basic design choices differently -- for
    example, the type-discrimination value (what you
    call 'Type' in your code) I would want to put in
    the structure being pointed to rather than in
    the pointer to the structure. However that's a
    much longer conversation, and not clearly about
    what's "right" or "wrong" but about different
    kinds of approaches to various design issues.
    If you're happy with how your code is going,
    and certainly it sounds like you are, I say
    just go ahead along the direction you're going.
    Later you can let the group know how it turned
    out. :)
     
    Tim Rentsch, Jan 15, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    690
    Matt Garman
    Apr 25, 2004
  2. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    924
    Chris Torek
    Apr 26, 2004
  3. Paminu
    Replies:
    5
    Views:
    660
    Eric Sosman
    Oct 11, 2005
  4. Michael Foukarakis

    Re: union of pointers to structs

    Michael Foukarakis, Jan 14, 2010, in forum: C Programming
    Replies:
    0
    Views:
    395
    Michael Foukarakis
    Jan 14, 2010
  5. Maciej Labanowicz

    [union] Pointers to inherited structs are valid ?

    Maciej Labanowicz, Jan 1, 2013, in forum: C Programming
    Replies:
    104
    Views:
    1,071
    Tim Rentsch
    Jan 25, 2013
Loading...

Share This Page