简体   繁体   English

函数定义与原型冲突类型

[英]function definition conflicting type with prototype

the definition of getGameInfo gives a conflicting type error with the prototype of itself. getGameInfo的定义与自身的原型产生冲突的类型错误。

#include <math.h>
#include <stdio.h>
//FUNCTION PROTOTYPES============================================
//print report title and column headers
void printHeaders(int year, int month, int day);

//print game info and footer with averages
void printGameInfo(int year, int month[], int day[], int auscore[], int oppscore[], int numGames);

//print footer with average and larges point spread
void printFooter(int auscore[], int oppscore[], int numGames);

//open and read file into 1D arrays and return #games
int getGameInfo(FILE* filePtr, int month[], int day[], int auscore[], int oppscore[]);

//return the average on an int array
double mean(int array[], int count);

int main()
{
    int month[15], day[15], auscore[15], oppscore[15], numGames;
#define thisyear 2017
#define lastyear 2016
    FILE *THISYEAR;  // pointer to data file
    FILE *LASTYEAR;  // pointer to data file
    THISYEAR = fopen("Auburn Football 2017.txt", "r");
    LASTYEAR = fopen("Auburn Football 2016.txt", "r");
    if (THISYEAR == NULL || LASTYEAR == NULL)  //bad open
        printf("Error opening input file.");
    else //good open
    {
        getGameInfo(LASTYEAR, month, day, auscore, oppscore);
        printGameInfo( lastyear, month, day, auscore, oppscore, numGames);
        //rest of program ...
    }
}

int getGameInfo(FILE* filePtr, int *month[], int *day[], int *auscore[], int *oppscore[])
{
    int numGames;
    for (numGames = 0; numGames < 14; numGames++)
    {
        fscanf(filePtr, "%d", &month[numGames]);
        fscanf(filePtr, "%d", &day[numGames]);
        fscanf(filePtr, "%d", &auscore[numGames]);
        fscanf(filePtr, "%d", &oppscore[numGames]);
    }
    return numGames;
}

void printGameInfo(int year, int month[], int day[], int auscore[], int oppscore[], int numGames)
{
    printHeaders(year, month[numGames], day[numGames]);
    printFooter(auscore, oppscore, numGames);
}

void printHeaders(int year, int month[numGames], int day[numGames])
{
    printf("%d Auburn Football Season as of %d/%d", &year, &month[numGames], &day[numGames]);
    printf("Date Auburn Opp");
}

void printFooter(int auscore[], int oppscore[], int numGames)
{
    double avoppscore, avauscore;
    avoppscore = mean(oppscore, numGames);
    avauscore = mean(auscore, numGames);
    printf(" Ave score %lf %lf ", avauscore, avoppscore);
}

double mean(int array[], int count)
{
    int i;
    double average, sum = 0;
    for (i = 0; i < count; i++)
    {
        sum += array[i];
    }
    average = sum / count;
    return average;
}

It is because your declaration calls for pointers, while your definition calls for double pointers. 这是因为您的声明要求使用指针,而定义要求使用双指针。

Compare your declaration and definition: 比较您的声明和定义:

int getGameInfo(FILE* filePtr, int month[], int day[], int auscore[], int oppscore[]);

VS VS

int getGameInfo(FILE* filePtr, int *month[], int *day[], int *auscore[], int *oppscore[])

Something like int *month[] is equivalent to int **month , while something like int month[] is equivalent to int *month when passing as an argument to a function. int *month[]等同于int **month ,而将int month[]等同于int *month作为参数传递给函数时。 See this answer . 看到这个答案

To fix, you can change your declaration to the following: 要解决此问题,您可以将声明更改为以下内容:

//open and read file into 1D arrays and return #games
int getGameInfo(FILE* filePtr, int *month[], int *day[], int *auscore[], int *oppscore[] );

or alternatively, change your function definition to the following: 或者,将函数定义更改为以下内容:

int getGameInfo( FILE* filePtr, int month[], int day[], int auscore[], int oppscore[])
{
    ...
}

Judging by what that function does, I believe you want the second option. 从该功能的作用来看,我相信您需要第二个选择。

every time compiler throws a conflicting type errors compare function prototype and definition. 每次编译器引发冲突的类型错误时,都要比较函数原型和定义。

in this case 在这种情况下

prototype 原型

int getGameInfo(FILE* filePtr, int month[], int day[], int auscore[], int oppscore[]);

and in definition 并在定义上

int getGameInfo(FILE* filePtr, int *month[], int *day[], int *auscore[], int *oppscore[])

as you can see for a given function with name getGameInfo arguments 2,3 and 4 doesn't match hence the error. 如您所见,名称为getGameInfo参数2,3和4的给定函数不匹配,因此出现错误。

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

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