gpt4 book ai didi

C 使用 gcc -Wall 给出一个数组下标有类型 'char' 错误

转载 作者:行者123 更新时间:2023-12-04 05:12:51 25 4
gpt4 key购买 nike

下面的代码是一个简单的后缀计算程序。虽然代码在没有 -Wall 选项的情况下工作得很好,但我似乎无法找到为什么它不适用于该选项。我有一个模糊的想法 -Wall 不允许我使用默认的有符号字符数组。因此,根据错误消息,postfix2.c: In function ‘main’: postfix2.c:45:3: warning: array subscript has type ‘char’我尝试声明 unsigned char input[13]相反。它没有解决问题。关于 -Wall 概念的任何指针,以及错误可能在哪里?谢谢。哦,让我自己更正代码,而不是简单地给我固定的代码!

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_DEPRECATE

int stack[100];
int top;

void push(int x){
top++;
stack[top] = x;
}

int pop(){
int temp = stack[top];
top--;
return(temp);
}

int main()
{
char input[13];
int integer, a, b, result;

while(1){
scanf("%s", input);

if (isdigit(input[0])) {
integer = atoi(input);
push(integer);
}

if (input[0] == '+'){
b = pop();
a = pop();
push (a+b);
}
else{
if (input[0] == '-'){
b = pop();
a = pop();
push (a-b);
}
else{
if (input[0] == '*'){
b = pop();
a = pop();
push (a*b);
}
}
}

if (input[0] == 'p'){
result = pop();
printf("%d\n", result);
}
}
}

最佳答案

尝试改变这个:
if (isdigit(input[0]))
到:
if (isdigit((unsigned char)input[0]))

if (isdigit((int)input[0]))
有关更多详细信息,请参阅类似问题:array subscript has type 'char'

关于C 使用 gcc -Wall 给出一个数组下标有类型 'char' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14672801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com