一个简单的C程序中有点深奥的gcc错误 我开始学习C++,作为一个练习,我正在编写一个简单的游戏。虽然我认为我的逻辑快结束了,但我不明白为什么gcc会抱怨我的代码
这是我的代码:一个简单的C程序中有点深奥的gcc错误 我开始学习C++,作为一个练习,我正在编写一个简单的游戏。虽然我认为我的逻辑快结束了,但我不明白为什么gcc会抱怨我的代码,c,gcc,C,Gcc,这是我的代码: #include <stdio.h> void readGuess(int[4]); int blackScore(int[4], int[4]); int anotherGame(void); int whiteScore(int[4],int[4]); void printScore(int[4],int[4]); int main(void){ int codes[5][4] = {{1,8,9,2}, {2,4,6,8}, {1,9,8,3}, {
#include <stdio.h>
void readGuess(int[4]);
int blackScore(int[4], int[4]);
int anotherGame(void);
int whiteScore(int[4],int[4]);
void printScore(int[4],int[4]);
int main(void){
int codes[5][4] = {{1,8,9,2}, {2,4,6,8}, {1,9,8,3}, {7,4,2,1}, {4,6,8,9}};
int i;
for (i=0; i<5; i++){
int guess[4];
readGuess(guess);
while(blackScore(guess, codes[i]) != 4) {
printScore(guess, codes[i]);
readGuess(guess);
}
printf("You have guessed correctly!!");
if(i<4){
int another = anotherGame();
if(!another)
break;
}
}
return 0;
}
void readGuess(int[4] guess) {
scanf("%d %d %d %d",&guess,&guess+1,&guess+2,&guess+3);
}
int blackScore(int[4] guess, int[4] code){
int score = 0;
int i;
for(i = 0; i<4;++i){
if(code[i]==guess[i]){
score++;
}
}
return score;
}
int whiteScore(int[] guess, int[] code){
int score = 0;
int i;
for(i = 0; i<4;++i){
int j;
for(j = 0; j<4;++j){
if(i!=j && (code[i] == guess[i])){
score++;
}
}
}
return score;
}
void printScore(int[4] guess, int[4] code){
printf("(%d,%d)\n",blackScore(guess,code),whiteScore(guess,code));
}
int anotherGame(void){
while(1){
printf("Would you like to play another game? [Y/N]\n");
char result;
result = getchar();
if(result == 'Y')
return 1;
else if (result == 'N')
return 0;
}
}
你们能帮我找出问题所在吗?将
int[4]guess
更改为int*guess
您的参数声明与Java混淆了,我想:
void readGuess(int[4] guess) {
应该是
void readGuess(int guess[]) {
其他函数也有同样的问题
此外,原型并不总是与实际函数匹配,例如,
whiteScore
您的参数不应该声明数组的大小。在参数中使用int guess[]
而不是int[4]guess
另外,
结果
需要在另一个游戏的顶部声明,除非您使用GCC的-c99
开关。int blackScore(int[4]guess,int[4]code){
看起来不对。这就解决了它!奇怪的是,我的注释中提到了这个函数(int[]arg)是通过引用传递数组的正确方法。@freefrag:然后扔掉你的笔记。没有否决你的意见,但是函数签名中的数组大小本身是没有意义的,编译器会忽略它们。因此,最好说int guess[]
正如其他一些人提到的,为了避免误导。@FatalError对于一维数组是的,但是对于多维数组仍然需要数组大小。更新了我的答案。
void readGuess(int guess[]) {