"function"错误的冲突类型 (C)

标签 c struct

我不断收到此错误:[错误] 'average_grade' 类型冲突 我找不到我的错误..我是 C 的新手,所以我真的需要一些帮助。

struct card {

    char on[20];
    char ep[20];
    float b;
    int ap;
    struct card *next;
};

struct card *first,*last,*t;

int ch;

int main()
{

    float mo;
    do {
        printf("\n1.Initialize\n2.Add to end\n3.Show list\n4.Average Grade\n0.Exit\nChoice:");
        scanf("%d",&ch);
        switch(ch) {
            case 1: init_list(&first,&last);
                    break;
            case 2: t=create_node();
                    add_to_end(t,&first,&last);
                    break;
            case 3: show_list(first);
                    break;
            case 4: mo=average_grade(&first);
                    printf("%f",&mo);
                    break;              
            case 0: printf("Bye!\n");
                    break;
            default:printf("Try again.\n");
                    break;
        } /* switch */
    } while (ch!=0);
    system("pause");
    return 0;
}

float average_grade(struct card *arxh)
{

    struct card *i;
    float sum=0;
    int cnt=0;
    for (i=arxh; i!=NULL; i=i->next)
    {
        sum= sum + i->b;
        cnt++;
    }
    return sum/cnt;
}
void init_list(struct card **arxh, struct card **telos)
{

    *arxh=NULL;
    *telos=NULL;
}

struct card *create_node()
{

    struct card *r;

    r=(struct card *)malloc(sizeof(struct card));
    printf("Give data:");
    scanf("%s %s %f %d",r->on,r->ep,&r->b,&r->ap);
    r->next=NULL;

    return r;
}

void add_to_end(struct card *neos,struct card **arxh,struct card **telos)
{

    if (*arxh==NULL)
    {
        *arxh=neos;
        *telos=neos;
    }
    else
    {
        (*telos)->next=neos;
        *telos=neos;
    }
} 

void show_list(struct card *arxh)
{

    struct card *i;

    for (i=first; i!=NULL; i=i->next)
        printf("%s %s %.1f %d\n",i->on, i->ep, i->b, i->ap);
}

最佳答案

在 C 中,如果在调用函数时没有找到可见的原型(prototype),编译器会使用 int 隐式声明原型(prototype)(C99 之前——从 C99 开始,隐式 int 规则已被删除)返回类型。

但是当稍后找到实际定义时,它们的类型(float)与为您声明的编译器冲突。因此,在文件开头声明函数原型(prototype)(或将它们放在头文件中)或将函数移至 main() 之上。

关于 "function"错误的冲突类型 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823556/

相关文章:

c++ - 如何在 C++ 中初始化 3D 数组

c - 有哪些现代 C 习惯用法/指南可以防止与内存相关的错误?

c - C 文件中无法识别 header 中定义的结构?

c++ - 是否有 QPair 类,但用于三个以上的项目而不是两个?

c - C 中的泛型函数指针

c - Emacs 正则表达式递增/递减数组索引

c - C 中大小为 1K 的列表的哈希实现

c - C中一对数据结构的不兼容指针类型

c# - 为什么 Nullable 类型必须有结构约束

go - 在 Golang 中将接口(interface){}转换为结构