Question regarding unions and structures 
Author Message
 Question regarding unions and structures

I have several structures, such as the following:

typedef struct MainStruct
{
   SpriteRec   spriteRec;

   short       oldRow;
   short       oldCol;
   short       direction;

Quote:
} MainStruct, *MainStructPtr;

typedef struct Enemy1Struct
{
   SpriteRec   spriteRec;

   short       direction;

Quote:
} Enemy1Struct, *Enemy1StructPtr;

typedef struct FallingStruct
{
   SpriteRec   spriteRec;

   short       startRow;
   short       fallDelay;
   short       tileID;
   short       bombTimer;
   short       life;

Quote:
} FallingStruct, *FallingStructPtr;

I want to be able to declare a new structure that is large enough to hold
the largest of any of those structures, but *not* including the SpriteRec.
Normally, you'd do something like a union for this, but a union would
include the SpriteRec:

typedef union SpriteUnion
{
   MainStruct     mainStruct;
   Enemy1Struct   enemy1Struct;
   FallingStruct  fallingStruct;

Quote:
} SpriteUnion;

Is there some way to declare some sort of a structure that is
sizeof(SpriteUnion) - sizeof(SpriteRec)? It seems this would be possible
in assembly language, but so far I haven't found any way to do it in C.

-Vern

--
Replace the REPLACEME in my email address with Jensen to reply.

Visit the SpriteWorld web page: http://www.*-*-*.com/
--



Mon, 12 Nov 2001 03:00:00 GMT  
 Question regarding unions and structures

Quote:

> I have several structures, such as the following:

<snip>

> Is there some way to declare some sort of a structure that is
> sizeof(SpriteUnion) - sizeof(SpriteRec)? It seems this would be possible
> in assembly language, but so far I haven't found any way to do it in C.

> -Vern

You could do:

typedef MyStruct {
        char data[sizeof(SpriteUnion) - sizeof(SpriteRec)];

Quote:
} MyStruct;

I'm not sure how useful this would be, though.  I'm having trouble
understanding why you want to do this.  I suspect there may be a
more elegant solution to your problem if I understood it better.

peter
--



Fri, 23 Nov 2001 03:00:00 GMT  
 Question regarding unions and structures

Quote:

> I have several structures, such as the following:

> typedef struct MainStruct {
>    SpriteRec   spriteRec;
>    [stuff]
> } MainStruct, *MainStructPtr;

> typedef struct Enemy1Struct {
>    SpriteRec   spriteRec;
>    [other stuff]
> } Enemy1Struct, *Enemy1StructPtr;

> typedef struct FallingStruct {
>    SpriteRec   spriteRec;
>    [different stuff]
> } FallingStruct, *FallingStructPtr;

> I want to be able to declare a new structure that is large enough to hold
> the largest of any of those structures, but *not* including the SpriteRec.

Well, if it was smaller than the whole thing, you couldn't really FIT an
enemy1Struct inside, huh?  So, you have to add an extra level of
granularity to your structs.  Make each of the non-SpriteRec parts of
each struct their own structs, and then union them together.  (I've
removed redundant Struct words for clarity):

        typedef struct FallingOnly { ... };
        typedef struct Enemy1Only  { ... };

        typedef struct Falling { Sprite s; FallingOnly fo };
        typedef struct Enemy1  { Sprite s; Eenmy1Only  e1o };

        typedef union SpriteUnion { FallingOnly fo; Enemy1Only e1o };

        SpriteUnion u;
        Falling f;
        u.fo = f.fo;      /* stuff the non-sprite stuff into the union */

I can't think of why you would want to do this, though.  Any
explanation?
Scott
--



Fri, 23 Nov 2001 03:00:00 GMT  
 Question regarding unions and structures

[snip]

Quote:
>typedef union SpriteUnion
>{
>   MainStruct     mainStruct;
>   Enemy1Struct   enemy1Struct;
>   FallingStruct  fallingStruct;
>} SpriteUnion;

>Is there some way to declare some sort of a structure that is
>sizeof(SpriteUnion) - sizeof(SpriteRec)? It seems this would be possible
>in assembly language, but so far I haven't found any way to do it in C.
>From what you're saying about sizes, I assume you mean so you can pass it

to malloc()?  If so, try this.

I'm not sure if this is portable (structure padding come to mind as a
possible portability-killer), but why not something like:

typedef union foo {
        MainStruct m;
        Enemy1Struct e;
        FallingStruct f;

Quote:
} bar ;

ptr = malloc(sizeof(bar) - sizeof(SpriteRec));

If that's not what you were looking for, then can you clarify?

-- DN
--



Fri, 23 Nov 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Structures and Unions Question.

2. fread w/ Union structures

3. Structures vs. Unions

4. Initialising Union members of Structures

5. Bit-fields, unions and structures...

6. structure, union & enumeration tags

7. help: unnamed unions within structures

8. Union within structures

9. Alignment of data in a Structure/Union

10. structure union mess

11. Problems with structure+union

12. Structures & Unions

 

 
Powered by phpBB® Forum Software