编译错误:'修改'的冲突类型 - 为什么?

时间:2016-12-14 10:42:35

标签: c

为什么我收到以下代码的编译错误。

#include <stdio.h>

void modify(struct emp *y);
struct emp
{
  char name[20];
  int age;
};
main()
{
  struct emp e={"foo",35};
  modify(&e);
  printf("\n%s%d",e.name,e.age);
}
void modify(struct emp *p){
  strupr(p->name);
  p->age = p->age+2;
}

以下是构建日志消息的一部分。

错误:'modify'|的冲突类型在第一行。 15

注意:之前的'修改'声明就在这里在第一行。 3

4 个答案:

答案 0 :(得分:2)

gcc可以告诉你原因。

提高警告级别:

prog.c:3:20: warning: 'struct emp' declared inside parameter list
 void modify(struct emp *y);
                    ^
prog.c:3:20: warning: its scope is only this definition or declaration, which is 
                      probably not what you want

您可以更改原型的顺序和结构定义,也可以在原型之前添加结构的声明。

答案 1 :(得分:1)

向上移动结构,以便您的函数原型知道emp

    struct emp
    {
      char name[20];
      int age;
    };
    void modify(struct emp *y);    

    int main(void)
    {
      struct emp e={"foo",35};
      modify(&e);
      printf("\n%s%d",e.name,e.age);
      return 0;
    }
    void modify(struct emp *p){
      p->age = p->age+2;
    }

答案 2 :(得分:1)

main()的声明不正确。函数should return an integerreturn 0;和返回类型应明确指定:

int main(void)
{
  /* ... */
  return 0;
}

modify函数的原型提到了未声明的结构emp。换句话说,您应该在原型之前放置emp结构的声明。

避免使用strupr作为is non-standard function

考虑声明函数static,如果它们应该在当前编译单元中仅使用

答案 3 :(得分:0)

移动定义将为您解决问题。但由于还没有人提到它,我会投入:

您可以使用向前声明struct的最小变化来修复它。

struct emp; // Forward declare struct emp before using it in the prototype.
void modify(struct emp *y);
相关问题