This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

cast to structure not working

Hi, I have this code working OK in IAR 5 kickstart and GNU toolchain:

typedef struct { uint8_t r; uint8_t g; uint8_t b;
} color;

void LCD_fill(uint16_t x, uint16_t y, color fcolor)
{ [...]

LCD_fill(640,480,(color){0xff,0,0});

However, MDK414 gives the following errors:
error: #119: cast to type "color" is not allowed
error: #29: expected an expression

were the last one stays when I don't cast.

I see two issues here:
1- the impossibility to cast to the 'color' type, can be removed by adding an indirection level (but I don't want to do that for performance reasons)
2- the impossibility to use a constant structure as the parameter (I also tried defining and casting as 'const' with no success).

However, everything works OK if I define

color RED={0xff,0,0}; // or const color, btw...

and do:

LCD_fill(640,480,RED);

This looks like RealView is playing nasty on me. Any clues ?

Parents
  • LCD_fill(640,480,(color){0xff,0,0});
    

    This is not supported by the C standard.

    Actually, it is. But it's a different C standard than most of us are used to.

    Contrary to the OP's belief, that's not actually a cast, either. It's a compound literal, one of the things that got added to the language as of the 1999 update of the C standard: ISO/IEC 9899:1999, usually referred to as C99.

    They standardized several existing extensions, and this was one of them. Others include designated initializers, // comments, complex numbers, a proper boolean type and size-specific integer types.

Reply
  • LCD_fill(640,480,(color){0xff,0,0});
    

    This is not supported by the C standard.

    Actually, it is. But it's a different C standard than most of us are used to.

    Contrary to the OP's belief, that's not actually a cast, either. It's a compound literal, one of the things that got added to the language as of the 1999 update of the C standard: ISO/IEC 9899:1999, usually referred to as C99.

    They standardized several existing extensions, and this was one of them. Others include designated initializers, // comments, complex numbers, a proper boolean type and size-specific integer types.

Children
No data