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

C语言报comparison between pointer and integer警告该如何修改

问题解决说明

警告触发原因

  • type_of_paper是字符数组,直接使用数组名时编译器会将其解析为数组首元素的指针地址,而'1'/'2'是字符型字面量(属于整数类),指针和整数直接比较不符合C语言语法规范,因此触发警告。
  • 同时你的代码还存在其他可修复的问题:
    • gets()为已废弃的不安全函数,存在缓冲区溢出风险
    • 代码中mark_1为笔误,你定义的变量为marks_1,运行时会报未定义变量错误
    • void main()不符合C语言标准主函数写法,可移植性差

修复方案

你可以直接比较数组第一个位置的字符,同时修正其他问题,修改后的完整代码如下:

#include <stdio.h>
#include <string.h>

char type_of_paper[20];
float marks_1, marks_2, Total_1_Mark, pp2_marks;

int main()
{
    printf("\nEnter type of paper [1/2] :");
    // 替换gets为fgets,避免缓冲区溢出
    fgets(type_of_paper, sizeof(type_of_paper), stdin);
    
    // 取数组第一个字符比较,类型匹配
    if(type_of_paper[0] == '1' )
    {
        printf("\nEnter Section A and Section B marks: ");
        // 修正笔误mark_1为marks_1
        scanf("%f %f", &marks_1, &marks_2);
        Total_1_Mark = marks_1 + marks_2;
        printf("\nPaper Type : Paper 1");
        //  %.2f保留两位小数更符合分数输出习惯
        printf("\nTotal paper 1 Mark : %.2f", Total_1_Mark);
    }
    else if(type_of_paper[0] == '2')
    {
        printf("\nEnter paper2 marks :");
        scanf("%f", &pp2_marks);
        printf("\nPaper Type : Paper 2");
        if(pp2_marks < 60)
        {
            printf("Status : noob");
        }
        else if (pp2_marks < 80)
        {
            printf("Status : Good");
        }
        else if (pp2_marks <= 100)
        {
            printf("Status : Excellent");
        }
        else printf("whats wrong with u");
    }
    else printf("type again");
    
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 00:18:02