错误:使用gcc的'six'的冲突类型 [英] error: conflicting types for ‘six’ with gcc
问题描述
接收错误:尝试编译时'6'的冲突类型。
void main(){
const int k = 4;
六(& k);
float * six(const int * x)
{
float * p = malloc(sizeof(float));
* p =(float)* x;
return p;
这是发生了什么事。
当编译器在调用它之前没有遇到函数的原型时,它会从调用本身推导出原型,并且假定返回类型为int。这就是它在你的例子中所做的。
后来它找到了函数的定义,它发现返回类型实际上是float,它与它早先推断的原型。因此,类型冲突的错误(而不是缺少原型)。
当然,解决方案是在函数调用之前为函数提供原型被制成。
Receiving error: conflicting types for ‘six’ when attempting to compile.
void main(){
const int k = 4;
six(&k);
}
float * six(const int *x)
{
float *p = malloc(sizeof(float));
*p = (float)*x;
return p;
}
Here is what is going on.
When the compiler does not encounter a prototype for a function before a call to it, it deduces the prototype from the call itself, and assumes the return type to be int. This is what it does in your example.
Later it finds the definition of the function, and it finds that the return type is actually float, which does not match with the prototype it has deduced earlier. Hence the error of conflicting types (instead of, say, missing prototype).
The solution is to, of course, provide a prototype for the function before a call to it is made.
这篇关于错误:使用gcc的'six'的冲突类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!