1

I'm trying to compile an existed project (which was designed for gcc) with armcc. For some reason I get the #119 error for the following casting:

(keyCert)(pCertHeader->flags)

I find it very odd because the flags variable is from type uint32_t, and keyCert type is actually uint32_t.

typedef union { 
        struct {
                uint32_t      a:4;
                uint32_t      b:28;
        }c;
        uint32_t      d;
} keyCert;

What could be the reason for this behavior? Note that I was able to compile it with gcc. Thanks!

Ofa
  • 67
  • 1
  • 10

1 Answers1

2

I find it very odd because the flags variable is from type uint32_t, and keyCert type is actually uint32_t.

Wrong, keyCert type is union {...}. Compiler doesn't know if you store struct c or uint32_t d in keyCert, so compiler cannot assume it is uint32_t. The reason GCC possibly doesn't throw any errors is because it is compiler extension. ISO C forbids this type of casting.

Even in GCC if you compile it using C99 strict mode, you will get the following message:

warning: ISO C forbids casts to union type [-Wpedantic]

user7771338
  • 185
  • 6
  • What about a struct without a union? Assume that I define keyCert as I defined c. Can't I cast a struct in a size of uint32_t to uint32_t? – Ofa Mar 28 '17 at 06:17
  • @Ofa. No, you cannot. ISO forbids any conversion from/to struct, union, array and matrix, so no matter what is in struct/union, you must read that value, and not convert whole struct to another type. Hope this helps. – user7771338 Mar 28 '17 at 07:43
  • Thanks! I've noticed that this version works: keyCert temp; temp.d = pCertHeader->flags; Now I can use temp.c – Ofa Mar 28 '17 at 08:01
  • @Ofa. Ok, but keep in mind that your code might be incompatible on some systems/version. Also, if you found my answer helpful, please consider accepting it. – user7771338 Mar 28 '17 at 08:10