1

i have a simple question: why does gcc say that i have a mismatch at the two lines?

WARNING: pointer/integer type mismatch in conditional expression
#define TEST(v) _Generic((v),   
    _Bool:          (v) ? "true" : "false", \
    char*:          (v) ? (v) : "null",  /* <- WARNING */ \
    const char*:    (v) ? (v) : "null",  /* <- WARNING */ \
    default:        (v))

    _Bool s = FALSE;
    printf(TEST(s));
    printf("\n");

    char *c = "string";
    printf(TEST(c));
    printf("\n");

    char *x = NULL;
    printf(TEST(x));
    printf("\n");

EDIT:

    float f = 5;
    printf(TEST(f));
    printf("\n");

this example will cause an

ERROR: type mismatch in conditional expression
why is float or double so different at this point?
Andi
  • 21
  • 2
  • It'd be nice if you could turn this into a [mcve], a complete source file that someone can copy and paste into a compiler and get the same error. I'd like to help, but it's too annoying to have to take this code and wrap it in a function, remember which header files to include, etc, etc, before I can even test it. – Nate Eldredge Sep 24 '20 at 14:06
  • At a glance, the last example with `float` seems that it will become `printf(f)` which is obviously no good, but I don't think that's the cause of the error you're seeing. – Nate Eldredge Sep 24 '20 at 14:08
  • yes you are right ... all branches will be checked: (bool) ? (bool) : (char*) could be the problem – Andi Sep 24 '20 at 14:12
  • `char*: (v) ? (v) : "null"` -->. `char*: (v) ? (char*)(v) : "null"` may reduce the problem. – chux - Reinstate Monica Sep 24 '20 at 17:03

0 Answers0