2017-11-04 60 views
0

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

函數:'printHeaders()'有一個未使用的參數:'month'建議消除該參數(或修復該函數以使用該參數,類似的考慮因素存在於參數'日'中 – user3629249

+0

類似於:'' fscanf(filePtr,「%d」,&month [numGames]);'正在傳遞一個'** int'參數,但是應該傳入一個'* int'參數建議刪除多餘的'&' – user3629249

+0

關於:'void printHeaders int year,int month [numGames],int day [numGames])'變量:'numGames'沒有被定義,在這裏建議:void printHeaders(int numGames,int year,int month [],int day [])調用'fopen()'時修改原型和所有對此函數的調用以匹配 – user3629249

回答

2

這是因爲你的聲明需要指針,而你的定義需要雙指針。

比較你的聲明和定義:

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功能。見this answer

要解決,您可以將您的聲明更改爲以下:

//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[]) 
{ 
    ... 
} 

通過該函數的作用來看,我相信你想要第二個選項。

0

每次編譯器拋出一個衝突類型錯誤比較函數的原型和定義。

在這種情況下

原型

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不匹配因此錯誤。