我正在努力理解数组/指针/函数,以及如何在我开始编程c代码时正确地应用它们。
这个程序根据所选的难度显示一串含有隐藏单词的字母几秒钟。然后它给你一个机会看看你是否找到了隐藏的单词。它这样做三次,然后游戏结束。
收到代码块编译器生成警告:(我在代码中包含了行号(在行的末尾),这样就很容易找到警告所指的行)
80 Warning: passing argument 3 of 'verifyanswer' makes pointer from integer without a cast
10 note: expected 'int* ' but argument is of type 'int' In function 'verifyanswer':
114 warning: comparison between pointer and integer
下面是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
void verifyanswer (char*StrGameArray, char response [], int Strlength []);// 10 10 10 10 10
int main () {
char *StringGameArray [50] = {"cathoropordscodiscopwnsestropthprds",//disco "cassormocedimahonahteidbballoonhelspq",//balloon "cheeszpistsrwhystacifitercinotonomwerd",};//why
int Stringlength [3] = {5,7,3};
char Response [80]={0};
int LevelChoice = 0; int DisplayTime = 0; int StartTime = 0; int Displayed = 0; int x = 0; system("clr");
while (LevelChoice !=4 )
{
printf("\nWELCOME TO THE FIND THE WORD GAME!");
printf("\nChoose your difficulty level 1-3");
printf("\n\n 1-Super easy");
printf("\n 2-Regular");
printf("\n 3-Hard");
printf("\n 4-Quit\n");
scanf("%d",&LevelChoice);
switch (LevelChoice)
{
case 1:
DisplayTime = 5;
break;
case 2:
DisplayTime = 3;
break;
case 3:
DisplayTime = 1;
break;
}//end of switch
StartTime = time(NULL);
for (x=0; x<3;x++)
{
//DISPLAY TEXT FOR A FEW SECONDS DICTATED BY DIFFICULTY CHOICE
while (StartTime + DisplayTime > time(NULL))
{
if (Displayed == 0)
{
printf("\nFind a word in :\n\n");
printf("%s\n\n",StringGameArray [x]);
Displayed = 1;
}//end if
}//end while
system("clear");
printf("\nEnter found word\n");
scanf("%s",Response);
verifyanswer(StringGameArray[x],Response,Stringlength [x]);//function call 80 80 80
Displayed = 0;
StartTime = time(NULL);
}//end for loop 3x
}//end of while
return 0;
}//end of function main
void verifyanswer (char*StrGameArray, char response [],int Strlength []) {
int LenghtofResponse = 0;
LenghtofResponse = (strlen(response));
printf("\nResponse is %s",response);
//CONVERT ANSWER TO LOWER CASE TO PERFORM A VALID COMPARISON int x =0; for (x=0; x<= strlen(response);x++){ response[x] = (tolower (response[x]));}
if ( (strstr(StrGameArray,response) != 0)&& (LenghtofResponse == Strlength) && (response[0] != 0)) //114 114 114 114 114
printf("\nCongratulations you found the word");
else printf("\nYou did not find the word");
}//end of check answer
2条答案
按热度按时间kyxcudwk1#
您已经声明并在稍后定义了函数以获取
int[]
这已经很可疑了,因为你会期望“length”是一个整数,而实际上,当你在
main()
中调用时,你传递给它一个int
这就是警告的含义。参数会被转换成一个指向int的指针,但这可能不是你想要的。为了解决这个问题,你应该声明并定义一个函数来接受int:
pn9klfpd2#
这里
你正在传递一个整数(
Stringlength [x]
)到verifyanswer
.函数需要一个数组