Function definition is somehow wrong.

huangapple go评论62阅读模式
英文:

Having trouble understanding C code build warnings. Function definition is somehow wrong

问题

我正在尝试理解数组/指针/函数以及如何在我的C代码初学编程中正确应用它们。

这个程序显示一堆字母,其中包含一个隐藏的单词,显示时间取决于所选择的难度。然后,它会给你一个机会来查看是否找到了隐藏的单词。这个过程会重复三次,然后游戏结束。

Code Blocks编译器生成的警告如下:
(我在代码中包含了行号(在行尾),以便轻松找到警告指向的行)

80 警告:传递给'verifyanswer'的第3个参数使指针从整数转换而来

10 注:预期为'int*',但参数的类型为'int' 在函数'verifyanswer'中:

114 警告:指针和整数之间的比较

以下是我的代码:

#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;
        }

        StartTime = time(NULL);

        for (x = 0; x < 3; x++) {
            while (StartTime + DisplayTime > time(NULL)) {
                if (Displayed == 0) {
                    printf("\nFind a word in :\n\n");
                    printf("%s\n\n", StringGameArray[x]);
                    Displayed = 1;
                }
            }
            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);
        }
    }
    return 0;
}

void verifyanswer(char *StrGameArray, char response[], int Strlength[]) {
    int LenghtofResponse = 0;
    LenghtofResponse = (strlen(response));
    printf("\nResponse is %s");

    for (int x = 0; x <= strlen(response); x++) {
        response[x] = tolower(response[x]);
    }

    if ((strstr(StrGameArray, response) != 0) && (LenghtofResponse == Strlength) && (response[0] != 0)) {
        printf("\nCongratulations you found the word");
    } else {
        printf("\nYou did not find the word");
    }
}
英文:

I am trying to understand arrays/ pointers/ function and how to properly apply them in my beginning programming of C code.

This program shows a bunch of letters with a hidden word in it for a few seconds depending on the difficulty selected. It then gives you an opportunity to see if you found the hidden word. It does this three times then the game ends.

Code Blocks Compiler build warnings received:
(I have included line numbers in the code (at the end of the line) so it is easy to find what line the warning is pointing to)

80 Warning: passing argument 3 of &#39;verifyanswer&#39; makes pointer from integer without a cast
10 note: expected &#39;int* &#39; but argument is of type &#39;int&#39; In function &#39;verifyanswer&#39;:
114 warning: comparison between pointer and integer

Here is my code:

#include &lt;stdio.h&gt; 
#include &lt;stdlib.h&gt; 
#include &lt;time.h&gt; 
#include &lt;string.h&gt;
#include &lt;ctype.h&gt;
void verifyanswer (char*StrGameArray, char response [], int Strlength []);// 10 10 10 10 10
int main () { 
char *StringGameArray [50] = {&quot;cathoropordscodiscopwnsestropthprds&quot;,//disco                &quot;cassormocedimahonahteidbballoonhelspq&quot;,//balloon &quot;cheeszpistsrwhystacifitercinotonomwerd&quot;,};//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(&quot;clr&quot;);
while (LevelChoice !=4 )
{
printf(&quot;\nWELCOME TO THE FIND THE WORD GAME!&quot;);
printf(&quot;\nChoose your difficulty level 1-3&quot;);
printf(&quot;\n\n 1-Super easy&quot;);
printf(&quot;\n 2-Regular&quot;);
printf(&quot;\n 3-Hard&quot;);
printf(&quot;\n 4-Quit\n&quot;);
scanf(&quot;%d&quot;,&amp;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&lt;3;x++)
{
//DISPLAY TEXT FOR A FEW SECONDS DICTATED BY DIFFICULTY CHOICE
while (StartTime + DisplayTime &gt; time(NULL))
{
if (Displayed == 0)
{
printf(&quot;\nFind a word in :\n\n&quot;);
printf(&quot;%s\n\n&quot;,StringGameArray [x]);
Displayed = 1;
}//end if
}//end while
system(&quot;clear&quot;);
printf(&quot;\nEnter found word\n&quot;);
scanf(&quot;%s&quot;,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(&quot;\nResponse is %s&quot;,response);
//CONVERT ANSWER TO LOWER CASE TO PERFORM A VALID COMPARISON int x =0; for (x=0; x&lt;= strlen(response);x++){ response[x] = (tolower (response[x]));}
if ( (strstr(StrGameArray,response) != 0)&amp;&amp;  (LenghtofResponse == Strlength) &amp;&amp; (response[0] != 0)) //114 114 114 114 114 
printf(&quot;\nCongratulations you found the word&quot;);
else printf(&quot;\nYou did not find the word&quot;);
}//end of check answer

答案1

得分: 1

你已经声明并定义了该函数以接受一个 int[]

void verifyanswer (char*StrGameArray, char response [], int Strlength []);

这已经引起了怀疑,因为你期望 "length" 应该是一个整数。而实际上,当你在 main() 中调用它时,你传递了一个 int

int Stringlength [3] = {5,7,3};
//...
verifyanswer(StringGameArray[x],Response,Stringlength [x]);

这就是警告的含义。参数会被转换为一个指向整数的指针,但这可能不是你的意图。要修复它,你应该声明并定义函数以接受一个整数:

void verifyanswer (char*StrGameArray, char response [], int Strlength);
英文:

You have declared, and later defined, the function to take a int[]

void verifyanswer (char*StrGameArray, char response [], int Strlength []);

That is already suspicious, because you would expect "length" to be an integer. And indeed, when you later call in in main(), you pass it an int

int Stringlength [3] = {5,7,3};
//...
verifyanswer(StringGameArray[x],Response,Stringlength [x]);

That is what the warning means. The argument would be converted to a pointer to an int, but that is probably not what you intended.To fix it you should be declaring, and defining, the function to take an int:

void verifyanswer (char*StrGameArray, char response [], int Strlength);

答案2

得分: 0

verifyanswer(StringGameArray[x], Response, Stringlength[x]);/

你正在向 verifyanswer 传递一个单独的整数 (Stringlength[x]),而该函数期望一个数组。

英文:

here

 verifyanswer(StringGameArray[x],Response,Stringlength [x]);/

you are passing a single int (Stringlength [x])to verifyanswer. the function expects an array

答案3

得分: 0

将以下内容从英文翻译为中文:

Change:

void verifyanswer (char*StrGameArray, char response [],int Strlength [])

to:

void verifyanswer (char*StrGameArray, char response [],int Strlength)
英文:

Change:

void verifyanswer (char*StrGameArray, char response [],int Strlength [])

to:

void verifyanswer (char*StrGameArray, char response [],int Strlength)

huangapple
  • 本文由 发表于 2023年3月7日 02:40:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定