Reputation: 6665
I have an enum, and a macro definition and a method that all use the enum. I can't get it to compile. Consider the following pieces of code.
typedef enum fruits_t
{
APPLE,
ORANGE,
BANANA
} fruits_t;
#define KEY_TO_VALUE(x) ((x == APPLE) ? 0 : \
(x == ORANGE) ? 1 : \
(x == BANANA) ? 2 : \
"Undefined")
static void foo(char fruit) {
if (fruit == KEY_TO_VALUE(APPLE)) {
/* do something */
}
}
This compiles, but I get the following warnings.
warning: pointer/integer type mismatch in conditional expression
warning: comparison between pointer and integer
Why? I am very new to C, so if you could explain things that may seem obvious to an experienced C developer, I'd appreciate it. Most of my programming knowledge is Java based.
Upvotes: 6
Views: 13293
Reputation: 320441
In C language the ?:
operator imposes the following requirements on its second and third operands
One of the following shall hold for the second and third operands:
- both operands have arithmetic type;
- both operands have the same structure or union type;
- both operands have void type;
- both operands are pointers to qualified or unqualified versions of compatible types;
- one operand is a pointer and the other is a null pointer constant; or
- one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void.
In your code you are mixing arithmetic type and pointer type (string literal decays to char *
type) as second and third operand of ?:
. This is not allowed.
Speaking informally, how did you even come up with that idea? What would be the type of something like x == BANANA ? 2 : "Undefined"
? int
? char *
? Something else?
Note that mixing a pointer value and a constant 0
is allowed, i.e. expression x == APPLE ? 0 : "Undefined"
would actually compile without any problems. However, the 0
would be interpreted as a null pointer constant, the whole expression would have pointer type (char *
) and evaluate to a null pointer when x == APPLE
is true. This is probably not what you intended.
Upvotes: 4
Reputation: 9480
The compiler is trying to figure out the type of each expression in the program.
An expression such as x > 0 ? 5 : "no"
makes the compiler scratch its head. If x is greater than zero, the type is int
, but if it isn't then the type is const char *
. This is a problem, because there is no automatic conversion from pointer to int
(and vice versa). So the compiler warns about it.
The solution is to make sure that no matter what the value of fruit
is, the value of KEY_TO_VALUE
has a single type. For example, instead of "Undefined" (which is of type const char *
, because it is a literal string), you can use a special value such as -1.
Also, note that APPLE
is a constant with the value 0, ORANGE
is a constant with the value 1 and BANANA
is a constant with the value 2 (this is how enum
works). So you don't need KEY_TO_VALUE
, as the constants already have the desired values. You can simply compare fruit
to APPLE
directly:
if (fruit == APPLE) { ... }
Upvotes: 14