为什么 Ideone.com C 编译器不捕获不匹配的指针类型?
Why doesn't the Ideone.com C compiler catch mismatched pointer types?
为什么我可以将错误类型的指针传递给 C 函数,
没有收到编译器错误或警告?
//given 2 distinct types
struct Foo{ int a,b,c; };
struct Bar{ float x,y,z; };
//and a function that takes each type by pointer
void function(struct Foo* x, struct Bar* y){}
int main() {
struct Foo x;
struct Bar y;
//why am I able to pass both parameters in the wrong order,
//and still get a successful compile without any warning messages.
function(&y,&x);
//compiles without warnings.
//potentially segfaults based on the implementation of the function
}
它不应该起作用。通过 gcc -Wall -Werror
编译失败。 From another post on SO, it's noted that Ideone uses GCC,因此他们可能使用了非常宽松的编译器设置。
示例构建
test.c: In function 'main':
test.c:15:2: error: passing argument 1 of 'function' from incompatible pointer type [-Werror]
function(&y,&x);
^
test.c:6:6: note: expected 'struct Foo *' but argument is of type 'struct Bar *'
void function(struct Foo* x, struct Bar* y){}
^
test.c:15:2: error: passing argument 2 of 'function' from incompatible pointer type [-Werror]
function(&y,&x);
^
test.c:6:6: note: expected 'struct Bar *' but argument is of type 'struct Foo *'
void function(struct Foo* x, struct Bar* y){}
^
test.c:19:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
cc1: all warnings being treated as errors
为什么我可以将错误类型的指针传递给 C 函数,
没有收到编译器错误或警告?
//given 2 distinct types
struct Foo{ int a,b,c; };
struct Bar{ float x,y,z; };
//and a function that takes each type by pointer
void function(struct Foo* x, struct Bar* y){}
int main() {
struct Foo x;
struct Bar y;
//why am I able to pass both parameters in the wrong order,
//and still get a successful compile without any warning messages.
function(&y,&x);
//compiles without warnings.
//potentially segfaults based on the implementation of the function
}
它不应该起作用。通过 gcc -Wall -Werror
编译失败。 From another post on SO, it's noted that Ideone uses GCC,因此他们可能使用了非常宽松的编译器设置。
示例构建
test.c: In function 'main':
test.c:15:2: error: passing argument 1 of 'function' from incompatible pointer type [-Werror]
function(&y,&x);
^
test.c:6:6: note: expected 'struct Foo *' but argument is of type 'struct Bar *'
void function(struct Foo* x, struct Bar* y){}
^
test.c:15:2: error: passing argument 2 of 'function' from incompatible pointer type [-Werror]
function(&y,&x);
^
test.c:6:6: note: expected 'struct Bar *' but argument is of type 'struct Foo *'
void function(struct Foo* x, struct Bar* y){}
^
test.c:19:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
cc1: all warnings being treated as errors