繁体   English   中英

函数定义与原型冲突类型

[英]function definition conflicting type with prototype

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[]);

VS

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的给定函数不匹配,因此出现错误。

暂无
暂无

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

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