pointer-to-const conversion in C

前端 未结 6 999
傲寒
傲寒 2021-01-13 20:09

The following code compiles without warning on GCC but gives a warning in Visual Studio 2005.

const void * x = 0;
char * const * p = x;

x p

6条回答
  •  长情又很酷
    2021-01-13 20:23

    warning C4090: 'initializing' : different 'const' qualifiers
    

    You cannot cast const void to char *const or even char *. By dereferencing p, you can now modify *(char *)(*x). This is a little known subtlety about pointers in C.

    A working type for p would be:

    char const **p = x;
    

    And yes, I put const on the right like a man.

提交回复
热议问题