簡體   English   中英

為什么我收到錯誤消息“s undeclared here not in a function”

[英]Why am i getting the error saying "s undeclared here not in a function"

正如你在這段代碼中看到的,我試圖從后綴轉換為中綴。

#include <stdio.h>
#include <stdlib.h>

    double compute(char symbol,double op1,double op2)

    {
        switch(symbol)
        {
            case '+': return op1+op2;
            case '-': return op1-op2;
            case '*': return op1*op2;
            case '$':
            case '^': return (op1,op2);
    
        }
    }
    main()
    {
        double s[20],res,op1,op2;
        int top,i;
        char postfix[20];
        char symbol;
        printf("Enter postfix");
        scanf("%s",postfix);
        top=-1;
        for(i=0;i<stlen(postfix);i++)
            symbol=postfix[i];
        if(isdigit(symbol))
            s[++top]=symbol='0';
        else
        {
            op2=s[top--];
            op1=s[top--];
        }
        res=compute(symbol,op1,op2);
        s[++top]=res;
    }
    res=s[top--];
    printf("res is %d",res);
}

我可能在這里遺漏了一些東西,因為它顯示s未聲明,但正如你所看到的,我在main()之后以 double 形式聲明了s

這三行不在任何范圍內:

res = s[top--];
printf("res is %d", res);
}

這就是為什么你有錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM