函数定义类型与原型冲突



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;
}

这是因为您的声明需要指针,而您的定义需要双指针。

比较您的声明和定义:

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

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

int *month[]这样的东西等价于int **month,而像int month[]这样的东西等价于作为参数传递给函数时的int *month。看到这个答案。

若要解决此问题,可以将声明更改为以下内容:

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

或者,将函数定义更改为以下内容:

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

从该功能的作用来看,我相信您想要第二种选择。

每次编译器抛出冲突的类型错误时,都会比较函数原型和定义。

在这种情况下

原型

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

和定义

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

如您所见,对于名称为 getGameInfo 参数 2、3 和 4 的给定函数,因此不匹配,因此存在错误。

最新更新