为什么 Ideone.com C 编译器无法捕获不匹配的指针类型?



为什么我可以将错误类型的指针传递给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编译失败。从另一个关于SO的帖子中,注意到Ideone使用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

最新更新