Keil Logo Arm Logo

Discussion Forum

enum type mixed with another type

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
barry gordon
Posted
23-Dec-2006 02:42 GMT
Toolset
ARM
New! enum type mixed with another type

I'm getting a warning "enumeration type mixed with another type" with the following line of code.

typedef enum { FALSE = 0, TRUE  = !FALSE } bool;

bool bResult;
bool bAAA;
bool bBBB;

bResult = bAAA && bBBB;  // <-- warning

bResult = (bAAA == TRUE) && (bBBB == TRUE); // <-- same results

Any suggestions?

Read-Only
Author
Andy Neil
Posted
23-Dec-2006 09:31 GMT
Toolset
ARM
New! RE: enum type mixed with another type

The is standard 'C' - nothing to do with Keil nor ARM.

Look up the && operator in your 'C' text book - what does it tell you about the types of the arguments that it expects and returns...?

Read-Only
Author
Andy Neil
Posted
23-Dec-2006 09:34 GMT
Toolset
ARM
New! Beware!
typedef enum
{
   FALSE = 0,      // Fine
   TRUE  = !FALSE  // Risky
} bool;

The 'C' language treats 0 as "false", but any non-zero value as "true"

Therefore beware of making any direct comparison to 'TRUE'...

Read-Only
Author
Andy Neil
Posted
23-Dec-2006 10:25 GMT
Toolset
ARM
New! RE: Beware!

http://c-faq.com/bool/index.html

Read-Only
Author
barry gordon
Posted
24-Dec-2006 07:08 GMT
Toolset
ARM
New! RE: enum type mixed with another type

I just reviewed ritchie & kernie. Section A7.14: "The && operator groups left-to-right. It returns 1 if both its operands compare unequal to zero, 0 otherwise."

The logic operation is returning a 1 or 0 and its being assigned to a bool (defined by the enum). If I perform the following, the warning is removed:

bResult = (bAAA && bBBB) ? TRUE : FALSE;


Since I'm including STR 73x_type.h in my code (located in my common project space), I made the following changes.

#ifdef __PROJECT__
typedef u16 bool;
#define FALSE 0
#define TRUE 1
#else
typedef enum { FALSE = 0, TRUE != FALSE } bool;
#endif


Compiles without the warning and my code should now be safer with this convension. __PROJECT__ is defined in the project file.

Thanks

Read-Only
Author
Andy Neil
Posted
24-Dec-2006 09:37 GMT
Toolset
ARM
New! RE: enum type mixed with another type

"The && operator ... returns 1 if both its operands compare unequal to zero, 0 otherwise."

That's the point - it takes arguments of type int, and returns a result of type int

The code you showed is using your "bool" enum type; not int - hence the compiler warns you,

"enumeration type mixed with another type"

The "other type" in this case is, of course, int.

You missed section A4.2 in K&R:

"Enumerations behave like integers, but it is common for a compiler to issue a warning when an object of a particular enumeration type is assigned something other than one of its constants, or an expression of its type"

The way to get around such warnings is always to use an appropriate explicit cast.

"code should now be safer with this convension"

Not really.
I don't think it makes any difference at all to the safety here, but it does have the disadvantage that the compiler can no longer produce symbolic debug information for the enum type.

For safety, I think the explicit cast is probably the way to go - as it makes your intention fully clear.

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Keil logo

Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.