You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C代码编译警告求助:函数参数与指针整数比较问题

解决C语言找单词游戏的编译警告问题

下面逐个分析并解决你遇到的三个编译警告:

警告1 & 警告2:参数类型不匹配(整数转指针)

问题原因

verifyanswer函数的声明(第10行)中,第三个参数定义为int Strlength[]——在C语言中,数组类型的形参会被隐式转换为指针int*,意味着函数期望接收一个整数数组的地址。但你在第80行调用函数时,传递的是Stringlength[x],这是数组Stringlength的单个元素,是整数类型,和函数期望的指针类型不匹配,因此触发类型转换警告。

解决步骤

  1. 修改函数声明,将第三个参数改为单个整数(因为你实际需要的是目标单词的长度值,不是数组):
    void verifyanswer(char* StrGameArray, char response[], int targetLength);
    
  2. 修改函数定义的参数列表,同步更新参数类型:
    void verifyanswer(char* StrGameArray, char response[], int targetLength) {
        // 函数内部代码保持不变,除了下面的条件判断
    }
    
  3. 修正函数内部的条件判断,将Strlength替换为targetLength:
    if ( (strstr(StrGameArray,response) != NULL) && (LengthofResponse == targetLength) && (response[0] != 0))
    
    (注:你的代码里LenghtofResponse拼写有误,建议改为LengthofResponse,不影响编译但更规范)

警告3:指针与整数之间的比较

问题原因

strstr函数的返回值是char*类型的指针:找到子串时返回子串的首地址,未找到时返回NULL(空指针常量)。你代码中写的strstr(StrGameArray,response) != 0是将指针和整数0直接比较,两者类型不兼容,触发警告。

解决步骤

将条件中的0替换为C语言标准的空指针常量NULL:

if ( (strstr(StrGameArray,response) != NULL) && (LengthofResponse == targetLength) && (response[0] != 0))

修正后的完整代码

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <string.h>
#include <ctype.h>

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

int main () { 
    char *StringGameArray[3] = {
        "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;
        }

        if (LevelChoice == 4) 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]);

            Displayed = 0;
            StartTime = time(NULL);
        }
    }
    return 0;
}

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

    // 将答案转为小写,确保比较不区分大小写
    int x = 0; 
    for (x = 0; x < strlen(response); x++) { 
        response[x] = tolower(response[x]);
    }

    if ( (strstr(StrGameArray, response) != NULL) && (LengthofResponse == targetLength) && (response[0] != 0)) {
        printf("\nCongratulations you found the word");
    } else {
        printf("\nYou did not find the word");
    }
}

(额外优化:修复了原代码中被注释的转小写逻辑,添加了退出选项的判断,避免选择退出后仍执行游戏循环)

内容的提问来源于stack exchange,提问作者Thirdeyedynamo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 05:54:59