Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:02:18+00:00 2026-06-14T11:02:18+00:00

I have an enum, and a macro definition and a method that all use

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:02:19+00:00Added an answer on June 14, 2026 at 11:02 am

    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) { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have enum list and method and i get error: not all code paths
I have an enum in my code that is the following: enum Status {In-Active,
I have a function-like macro that takes in an enum return code and a
I have this bit of code that helps me convert enum to string and
I have this macro code, which allows me to define both a C enum
I have run into a problem while writing C++ code that needs to compile
I have multiple enum's that all have the same constructor and attributes, like this:
I have some existing code that uses an enum instead of a function pointer
I have following Enum in my project public enum CameraAccessMethod { Manual = 0,
I have the following 2 macros: #define SCOPED_ENUM_HEADER(NAME) struct NAME{ enum _NAME{ #define SCOPED_ENUM_FOOTER(NAME)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.