2013-02-03 22 views
1

下面的代码是一个简单的postfix计算程序。尽管没有-Wall选项,代码工作得非常好,但我似乎无法找到为什么它不能使用该选项。我有一个模糊的想法 - 墙不允许我使用默认的signed char数组。因此,根据错误讯息,postfix2.c: In function ‘main’: postfix2.c:45:3: warning: array subscript has type ‘char’我尝试声明unsigned char input[13]。它没有解决这个问题。任何关于-Wall概念的指针,以及错误可能在哪里?谢谢。哦,让我自己纠正代码,不要简单地给我固定的代码!使用gcc -Wall给出的数组下标有'char'类型的错误

#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); 
     } 
    } 
} 
+0

这不是真正的代码。 – cnicutar

+0

先提供真实码 –

+0

我看到我贴的代码。你们不看代码吗?我应该发布一个主要链接吗? –

回答

2

尝试修改此:

if (isdigit(input[0]))

到:

if (isdigit((unsigned char)input[0]))

if (isdigit((int)input[0]))

更多详情查看类似的问题:array subscript has type 'char'

+0

完美工作。谢谢。我没有想到为了防止负面的索引 –

+0

它是c宏的混乱和isdigit()更具体,他们不警告友好至少可以说 – talkol

1

这意味着数组的索引可以是负数。

这可能是一个问题,因为字符可能代表一个有符号的值,并且您可能正在请求负指数。

相关问题