函数参数为指针,但实际传入值类型为什么不会报错?
问题:1、C 为什么不严格检查参数类型
2、为什么 参数是 int [] 传入 int 是警告,而 double [] 传入 double 是报错
#include <stdio.h>
int a(int []);
int main(void)
{
int b = 10;
int s = a(b);
printf("%d\n", s);
return 0;
}
int a(int s[])
{
return s;
}
// 编译器警告如下
test.c:7:14: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'int *'; take the address with & [-Wint-conversion]
int s = a(b);
^
&
test.c:2:11: note: passing argument to parameter here
int a(int []);
^
test.c:15:11: warning: incompatible pointer to integer conversion returning 'int *' from a function with result type 'int'; dereference with * [-Wint-conversion]
return s;
^
*
2 warnings generated.
// 结果输出
10
//----------------
#include <stdio.h>
int a(double []);
int main(void)
{
double b = 10;
int s = a(b);
printf("%d\n", s);
return 0;
}
int a(double s[])
{
return s;
}
// 编译器报错
test.c:7:14: error: passing 'double' to parameter of incompatible type 'double *'; take the address with &
int s = a(b);
^
&
test.c:2:14: note: passing argument to parameter here
int a(double []);
^
test.c:15:11: warning: incompatible pointer to integer conversion returning 'double *' from a function with result type 'int' [-Wint-conversion]
return s;