UNION and STRUCT

I'd like to make a definition of union in one file and call it from others. Union needs to consists structures in it. Is it possible?
Thank you,
Goran

Parents
  • Yes. Just put the declaration in a header file. #include the header file in the files that want to use the type.

    MyUnion.h:

    typedef struct
        {
        U8 blah;
        U8 bleah;
        } StructOne;
    
    typedef struct
        {
        U16 foo;
        U16 bar;
        } StructTwo;
    
    typedef union
        {
        StructOne asStructOne;
        StructTwo asStructTwo;
        } MyUnion;
    
    

    MyUnionClient.c:
    #include "MyUnion"
    
    MyUnion mine;
    ...
    mine.asStructOne.blah = 0;
    

Reply
  • Yes. Just put the declaration in a header file. #include the header file in the files that want to use the type.

    MyUnion.h:

    typedef struct
        {
        U8 blah;
        U8 bleah;
        } StructOne;
    
    typedef struct
        {
        U16 foo;
        U16 bar;
        } StructTwo;
    
    typedef union
        {
        StructOne asStructOne;
        StructTwo asStructTwo;
        } MyUnion;
    
    

    MyUnionClient.c:
    #include "MyUnion"
    
    MyUnion mine;
    ...
    mine.asStructOne.blah = 0;
    

Children
More questions in this forum