简体   繁体   English

错误:与gcc的“六个”类型冲突

[英]error: conflicting types for ‘six’ with gcc

Receiving error: conflicting types for 'six' when attempting to compile. 接收错误:尝试编译时'six'的类型冲突。

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. 当编译器调用函数之前没有遇到函数的原型时,它会从调用本身中推导出原型,并假定返回类型为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. 解决方案当然是在对该函数进行调用之前为其提供原型。

You didn't declare six to the compiler before you called it, so the compiler was forced to guess what the signature of six is (typically, this is something like int func() ). 您在调用编译器之前没有向其声明six ,因此编译器被迫猜测six的签名是什么(通常,这类似于int func() )。 When it saw the actual declaration, it threw an error because the actual function declaration didn't match its implicit declaration. 当看到实际的声明时,它抛出了错误,因为实际的函数声明与它的隐式声明不匹配。

You need to declare functions before they are used; 您需要在使用函数之前声明它们; place a declaration like

float *six(const int *x);

before main . main

Solution to your problem 解决您的问题

Just add the following declaration before main() : 只需在main()之前添加以下声明:

float *six(const int *x);

Or put your float *six(const int *x) definition before the main() function. 或者将您的float *six(const int *x)定义放在main()函数之前。

Why the compiler complain conflicting types 为什么编译器抱怨conflicting types

Since there is no declaration of your six() function before the compiler compile the main function, it will deduce the prototype from the function callsite, and will assume the return type to be int . 由于在编译器编译主函数之前没有声明您的six()函数,因此它将从函数调用站点推断出原型,并假定返回类型为int And when it compiles your six() function, the compiler will find two function with the same name but different return type, so it complain the conflicting types error. 当编译您的six()函数时,编译器会发现两个具有相同名称但返回类型不同的函数,因此会抱怨conflicting types

Thanks to Ziffusion's comment. 感谢Ziffusion的评论。

why to adjust your code in the above way 为什么以上述方式调整代码

You should declare/define each of your element before use in C. 在C中使用之前,应声明/定义每个元素。

For your currently code, you need to declare your function type before the main function, so that the compiler knows what six() is when compile the main function. 对于当前代码,您需要在主函数之前声明函数类型,以便编译器在编译main函数时知道six()是什么。

Why there should be a declaration before use in C 为什么在C中使用之前应该有一个声明

For variables and other data types, since C is strong typed. 对于变量和其他数据类型,由于C是强类型。 When the variable is used, it need to be declared first, so that the compiler knows what type the variable is, and could do data type check. 使用该变量时,需要先声明该变量,以便编译器知道该变量是什么类型,并可以进行数据类型检查。

For functions, since the C compiler compiles the code function by function, and will generate a function call statement in the assembly, so the compiler need to know the function parameter and return value data type, so that it could generated correct instructions to do parameter passing, and return value restoring. 对于函数,由于C编译器按函数编译代码函数,并将在程序集中生成函数调用语句,因此编译器需要知道函数参数和返回值数据类型,以便可以生成正确的指令来执行参数传递,并返回值恢复。 Normally, the compiler will compile the function in a source code file one by one from the start of the file to the end of the file, so you need to declare the function type before use it. 通常,编译器将从文件的开始到文件的末尾在源代码文件中一一编译函数,因此在使用函数之前,需要声明函数类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM