Should clang and gcc produce a diagnostic message when a program does pointer arithmetic on a function pointer?

后端 未结 2 1346
我在风中等你
我在风中等你 2021-01-22 07:49

This program compiles without errors, for example with clang -Wall -std=c11 a.c and gcc -Wall -std=c11 a.c. Is this a bug in clang and gcc? Because ari

2条回答
  •  迷失自我
    2021-01-22 07:53

    Add the -Wpointer-arith flag to produce warnings

    a.c:8:22: warning: arithmetic on a pointer to the function type 'void (void)' is a GNU extension [-Wpointer-arith]
        printf("%p\n", p + 1);
                       ~ ^
    

    From GNU documentation

    In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.

    Clang -Wpointer-arith documentation

    warning: arithmetic on a pointer to the function type B is a GNU extension

提交回复
热议问题