0

Given the following code

#include <stdio.h>

typedef enum{
    ISP_INPUT_NONE,
    ISP_INPUT_DOLBY
} UserStream_t;

int someFunc(const char * str) {
    printf("%s", str);
    return 0;
}

int main(int argc, char** arg)
{
    int a = ISP_INPUT_NONE;
    someFunc(ISP_INPUT_NONE);
    someFunc(a);
    return 0;
}

The second call triggers an integer conversion warning, but the first doesn't.

gcc -Wall test.c
test.c: In function ‘main’:
test.c:17:14: warning: passing argument 1 of ‘someFunc’ makes pointer from integer without a cast [-Wint-conversion]
   17 |     someFunc(a);
      |              ^
      |              |
      |              int
test.c:8:27: note: expected ‘const char *’ but argument is of type ‘int’
    8 | int someFunc(const char * str) {
      |              ~~~~~~~~~~~~~^~~

Are enum silently converted to pointers?

I thought C enums were considered as integers. I would like the first call to generate the same warning as the second.

5
  • "The first call triggers an integer conversion warning, but the second doesn't." According to what you posted, it is the second call (the one using an integer variable) that is triggering the warning and the first (using an enum value literally) that passes silently. Was this a typo? Commented Mar 29, 2021 at 11:44
  • Now try with ISP_INPUT_DOLBY :D . ISP_INPUT_NONE is 0, so it's implicitly converted to void*. Commented Mar 29, 2021 at 11:44
  • So zero valued enum are silently considered as NULL pointer, but zero valued int is not ? Commented Mar 29, 2021 at 11:49
  • 2
    @shodanex: An enumeration constant is an int. Both an enumeration constant with value zero and the integer constant 0 qualify as a null pointer constant. (This, “null pointer constant,” is the proper term. NULL is the name of a macro.) Commented Mar 29, 2021 at 12:06
  • 1
    GCC 10.2 appears unable to warn about this. Clang 11.0 does, with -Wnon-literal-null-conversion, which is on by default. You should specify the compiler you are using. Commented Mar 29, 2021 at 12:08

1 Answer 1

3

Are enum silently converted to pointers ?

Well, some of them. ISP_INPUT_NONE is 0 and enumeration values are constant expressions and a constant expression with the value 0 is a null pointer constant. And converting a null pointer constant to another pointer is just normal.

The warning will still be issued if the enum value is not 0, for example for someFunc(ISP_INPUT_DOLBY);.

How to trigger integer conversion warning when passing enum instead of pointer

Ideas:

Start enum with 1 instead of 0.

typedef enum {
    ISP_INPUT_NONE = 1,
    ISP_INPUT_DOLBY
} UserStream_t;

Use a structure and have conversion errors.

typedef struct {
   unsigned char val;
} UserStream_t;
static const UserStream_t ISP_INPUT_NONE = {0};
static const UserStream_t ISP_INPUT_DOLBY = {1};

Some way use a macro so that ISP_INPUT_NONE is not a constant expression.

static int entity(int a) { return a; }

typedef enum{
    ISP_INPUT_NONE,
#define ISP_INPUT_NONE (entity(ISP_INPUT_NONE))
    ISP_INPUT_DOLBY
} UserStream_t;

Use clang compiler with -Wnon-literal-null-conversion option.

In g++ there is -Wzero-as-null-pointer-constant option, but no such option for C in gcc. You could also make a feature request to gcc.

Sign up to request clarification or add additional context in comments.

2 Comments

The fact that ISP_INPUT_NONE is a null pointer constant does not mean a compiler cannot issue a diagnostic message for its use as a pointer. Clang does.
Re “a constant expression with the value 0 just is a null pointer by itself”: Not quite. It is a null pointer constant. If 0 were a null pointer, int x = 0; would be invalid, as the initializer for an int cannot be a pointer. A null pointer constant is not a pointer; it is a type of constant that can be accepted in certain places where a pointer is otherwise required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.