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

后端 未结 2 1345
我在风中等你
我在风中等你 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

    0 讨论(0)
  • 2021-01-22 08:03

    Function pointer arithmetic is a gcc extension also implemented by clang.

    To invoke standards compliance you need

    gcc -std=c11 -pedantic ...
    clang -std=c11 -pedantic ...
    
    0 讨论(0)
提交回复
热议问题