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:
- This is legal C.
It would be pretty annoying in other contexts, e.g.
int *array = malloc(5 * sizeof(*array))
.
精彩评论