开发者

GCC warning on implicit casting of void* to another pointer type

As t开发者_JAVA百科he title says, is there a way to force GCC to warn me when I do something like this:

void do_something(int* ptr)
{
    // do something
}

int main()
{
    int a = 123;
    void* b = &a;

    // WARN HERE:
    do_something(b);
}


Use -Wc++-compat. From the GCC manual:

-Wc++-compat (C and Objective-C only)

Warn about ISO C constructs that are outside of the common subset of ISO C and ISO C++, e.g. request for implicit conversion from void * to a pointer to non-void type.


Two reasons why the answer is probably no:

  1. This is legal C.
  2. It would be pretty annoying in other contexts, e.g.

    int *array = malloc(5 * sizeof(*array)).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜