我的代码出现以下错误,其中一个我无法找到解决方法。我理解函数类型的声明必须与定义匹配,但确实如此。
#include <stdio.h>
#include <math.h>
//Global Declarations
int input_win(void);
int input_scores(int,int);
void results(int,int,int,int);
int main()
{
//Local Declarations
int winNum; //The number of points needed to win
int scores[25]; //The array of scores
int score_old = 0; //The second to most recent score input
int score_new; //The most recent score input
int cnt_score = 1; //The count for input
int cnt_won = 0; //The count for games won
int cnt_played =0; //The count for games played
//Executeable Statements
winNum = input_win();
do
{
score_new = input_scores(cnt_score,cnt_won);
if(score_new != -1)
{
if(score_new <= score_old)
{
cnt_played += 1;
}
if(score_new == winNum)
{
cnt_won += 1;
}
scores[cnt_score - 1] = score_new;
score_old = score_new;
cnt_score += 1;
}
}while(score_new != -1 || score_new != 25);
results(cnt_score,scores,cnt_won,cnt_played);
return 0;
}
...
void results(int cnt_score,int scores[25],int cnt_won,int cnt_played)
{
//Local Declarations
int cnt_loop; //The counter for the for loop
//Executeable Statements
printf("\nScores entered (%d): {",cnt_score);
for (cnt_loop = 1;cnt_loop < cnt_score;cnt_loop++)
{
printf("%d, ",scores[cnt_loop - 1]);
}
printf("%d}\n",scores[cnt_score - 1]);
printf("\nNumber of games won: %d\nNumber of games played: %d\n",cnt_won,cnt_played);
}
我得到的错误如下:
hw06.c: In function 'main':
hw06.c:62: warning: passing argument 2 of 'results' makes integer from pointer without a cast
hw06.c:29: note: expected 'int' but argument is of type 'int *'
hw06.c: At top level:
hw06.c:160: error: conflicting types for 'results'
hw06.c:29: note: previous declaration of 'results' was here
答案 0 :(得分:2)
您的函数原型与您的函数定义不匹配:
void results(int,int,int,int);
void results(int cnt_score,int scores[25],int cnt_won,int cnt_played) { ... }
请注意,第二个参数是int *
,而不是int
。
答案 1 :(得分:1)
将结果函数重新声明为:
void results(int,int[],int,int);
答案 2 :(得分:0)
函数的第二个参数是使用类型int
声明的,但您的实现是int[25]
类型,是一个整数数组。