簡體   English   中英

“功能”錯誤的類型沖突(C)

[英]Conflicting types for “function” error (C)

我不斷收到此錯誤:[錯誤]'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語言中,如果在調用函數時找不到可見的原型,則編譯器會使用int返回類型隱式聲明原型(C99之前的版本-自C99起,隱式int規則已被刪除)。

但是,當稍后找到實際的定義時,它們的類型( float )與為您聲明的編譯器沖突。 因此,在文件的開頭聲明函數原型(或將它們放在頭文件中)或將函數移到main()上方。

由於您沒有傳遞更多信息,因此我懷疑這里的錯誤:

 struct card *first ... mo=average_grade(&first) ... float average_grade(struct card *arxh) 

您將struct card ** (“指向struct ..的指針”)傳遞給需要struct card * (“指向struct ..的指針”)的函數。

由於不更改arxh ,因此可能需要mo=average_grade(first)

注意,缺少原型。 我想你是在發布代碼之前給出的。

注意:您應該始終發布MCVE 這個例子遠非如此。 您也不會顯示是否/試圖發現自己。

暗示:

始終啟用警告。 對於您的編譯器,至少-Wall (對於gcc)或類似版本。 更多警告可能會有所幫助,請檢查編譯器的可用選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM