pointer-to-const conversion in C

前端 未结 6 940
傲寒
傲寒 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:30

    The C code is valid and a conforming compiler shouldn't warn as const ness is correctly preserved and conversion of void * to any pointer type (function pointers aside) is implicit.

    A C++ compiler is supposed to warn about the implicit conversion, but a warning about discarding the const qualifier is wrong and should be considered a compiler bug.

提交回复
热议问题