产生conflicting types for的两个原因

(一)首先我们看一个函数的定义和声明不一致的例子:

#include <stdio.h>

int func(int a);

int func(void) {
    return 0;
}

int main(void) {

    func();

    return 0;
}

编译程序:

gcc -g -o a a.c
a.c:5:5: error: conflicting types for ‘func’
int func(void) {
^
a.c:3:5: note: previous declaration of ‘func’ was here
int func(int a);
可以看到由于“func”的声明和定义不一致(一个有参数,一个没有),所以编译时出现了这个错误。

(二)最近我移植一部分代码到jffs2的时候,编译时也出现了这个错误。但是我发现函数在头文件里的声明和函数定义是完全一样的,这就令我很奇怪。最后得到结论是函数参数类型在函数声明后定义了。简化的代码如下:

#include <stdio.h>

void func(struct A *A);

struct A {
        int a;
};

void func(struct A *A)
{
        printf("%d", A->a);
}

int main(void) {
        // your code goes here
        struct A a = {4};
        func(&a);
        return 0;
}

其中“structure A”的定义放在“func”函数声明之后了,而func函数的参数是“structure A*”类型。编译结果如下:


gcc -g -o a a.c
a.c:3:18: warning: ‘struct A’ declared inside parameter list
 void func(struct A *A);
                  ^
a.c:3:18: warning: its scope is only this definition or declaration, which is probably not what you want
a.c:9:6: error: conflicting types for ‘func’
 void func(struct A *A)
      ^
a.c:3:6: note: previous declaration of ‘func’ was here
 void func(struct A *A);
      ^

可以看到也输出了“error: conflicting types for ‘func’”的编译错误,也许编译警告可以给一点提示吧。

  • 16
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值