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

时间:2015-08-18 16:27:38

标签: c function segmentation-fault static-analysis

为什么我能够将错误类型的指针传递给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
}

See on Ideone

1 个答案:

答案 0 :(得分:4)

它不应该工作。通过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