This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Union/structure initializer

Hello,

I am working with TMS570 controller with TI ARM Compiler V5.1.6.

I have to initialize a union which is as below.

===========================================================

union

{

     struct

{

 bitfield bit1:1;

 bitfield bit2:1;

............

}Bits;

      u16 word;

}uniontype;

======================================================

Now, is it possible to initialize all the elements of the above union object to zero as below.

 

uniontype myunion = {0};

 

As per my knowledge, this type of initialization is allowed in C99. Is the TI compiler version given above supports this?

If not propose me a simple way to initialize the local structure/union objects to zero.

 

Thanking you

Regards

Giri Chavana

 

  • Giri,

    This operation is not supported by Texas Instruments compiler.

    You can write a function to clean every item defined in your structure. Another option is to define a data pointer (8 bit/ 16 bit/ 32 bit, depending on the size of your structure) and assign the pointer to the start address of your structure.

    Thanks and regards,

    Zhaohong
  • Hi Zhaohong,

    My processor is RM48.

    I have a union defined as

    typedef union zMIN
    {
        float32 f;
        uint32 ul;
        uint16 ui;
    }tzMIN;

    and I need to initialize the union as const and have it stored in FLASH. How can I achieve something as such.

    static const tzMIN min1 = { .f = -100.0f };

    static const tzMIN min2 = { .ul = 1u };

    The reason I need this to work is because tzMIN is an element in a big struct which may contain different data type, can need to be validated for its min value.

    Thanks for your help.

    Clio