C 我在第210行和第135行得到一个未初始化的局部变量错误

C 我在第210行和第135行得到一个未初始化的局部变量错误,c,postfix-mta,infix-notation,C,Postfix Mta,Infix Notation,在这个将中缀转换为后缀的程序中,我在210行和135行遇到了一个未初始化的局部变量错误。你能看出我做错了什么吗 /* 1) infix: A*B+C postfix: AB*C+ 2) infix: A+B*C postfix: ABC*+ 3) infix: A*B+C*D postfix: AB*CD*+ 4) infix: A*B^C+D postfix: ABC^*D+ 5) infix: A*(B+C*D)+E postfix: ABC

在这个将中缀转换为后缀的程序中,我在210行和135行遇到了一个未初始化的局部变量错误。你能看出我做错了什么吗

/*
1)
infix:  A*B+C
postfix: AB*C+

2)
infix:      A+B*C
postfix: ABC*+

3)
infix:      A*B+C*D
postfix: AB*CD*+

4)
infix:      A*B^C+D
postfix: ABC^*D+

5)
infix:      A*(B+C*D)+E
postfix: ABCD*+*E+

6)
infix:      A+(B*C-(D/E^F)*G)*H
postfix:    ABC*DEF^/G*-H*+

7)
infix:      (A+B)*C+D/(E+F*G)-H
postfix: AB+C*DEFG*+/+H-

8)
infix:      A-B-C*(D+E/F-G)-H
postfix:    AB-CDEF/+G-*-H-

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

//*******************************************
//** STACK
#define size 10

struct stack {

    int count;
    char stack[size];
} s;

void stack_push(char c) {

    if (s.count < size) {

        s.stack[s.count] = c;
        s.count = s.count + 1;
    }
}

char stack_pop() {

    char item;

    if (s.count > 0) {

        s.count = s.count - 1;
        item = s.stack[s.count];
    }

    return item;
}

int stack_isEmpty() {

    return s.count == 0;
}

char stack_topChar() {

    return s.stack[s.count - 1];
}

//*******************************************
//** Aux operations
int isOperand(char c) {

    return c >= 'A' && c <= 'Z';
}

int isOperator(char c) {

    char* operators = "+-*/^\0";

    int result = 0;

    for (int i = 0; operators[i] != '\0'; i++) {

        if (operators[i] == c) {

            result = 1;
            break;
        }
    }

    return result;
}

int getPrecedence(char c) {



    int result = 0;

    switch (c) {

    case '^': result++;
    case '/':
    case '*': result++;
    case '-':
    case '+': result++;
    }

    return result;
}
//*******************************************
//** to Postfix
void toPostfix(char* expression) {

    char* result;
    int idx = 0;

    for (int i = 0; expression[i] != '\0'; i++) {

        char c = expression[i];

        if (isOperand(c)) {

            result[idx++] = c;
        }
        else if (isOperator(c)) {

            char topChar;

            while (1) {

                topChar = stack_topChar();

                if (stack_isEmpty() || topChar == '(') {

                    stack_push(c);
                    break;
                }
                else {

                    int precedenceC = getPrecedence(c);
                    int precedenceTC = getPrecedence(topChar);

                    if (precedenceC > precedenceTC) {

                        stack_push(c);
                        break;
                    }
                    else {

                        char cpop = stack_pop();
                        result[idx++] = cpop;
                    }
                }
            }
        }
        else if (c == '(') {

            stack_push(c);
        }
        else if (c == ')') {

            char cpop = stack_pop();

            while (cpop != '(') {

                result[idx++] = cpop;
                cpop = stack_pop();
            }
        }
    }

    while (!stack_isEmpty()) {

        char c = stack_pop();
        result[idx++] = c;
    }

    result[idx] = '\0';
    printf("%s", result);
}

//*******************************************
//** main
int main() {

    printf("Insert expression: ");
    char* expression;
    char c;
    int idx = 0;

    do {

        c = getchar();

        if (c == '\n' || c == EOF)
            c = '\0';

        expression[idx++] = c;
    } while (c != '\0');

    toPostfix(expression);

    return 0;
}

/*
1)
中缀:A*B+C
后缀:AB*C+
2)
中缀:A+B*C
后缀:ABC*+
3)
中缀:A*B+C*D
后缀:AB*CD*+
4)
中缀:A*B^C+D
后缀:ABC^*D+
5)
中缀:A*(B+C*D)+E
后缀:ABCD*+*E+
6)
中缀:A+(B*C-(D/E^F)*G)*H
后缀:ABC*DEF^/G*-H*+
7)
中缀:(A+B)*C+D/(E+F*G)-H
后缀:AB+C*DEFG*+/+H-
8)
中缀:A-B-C*(D+E/F-G)-H
修复后:AB-CDEF/+G-*-H-
*/
#包括
#包括
//*******************************************
//**堆叠
#定义尺寸10
结构堆栈{
整数计数;
字符堆栈[大小];
}s;
无效堆栈推送(字符c){
如果(s.计数<大小){
s、 堆栈[s.count]=c;
s、 计数=s.count+1;
}
}
char stack_pop(){
字符项;
如果(s.计数>0){
s、 计数=s.count-1;
项目=s.stack[s.count];
}
退货项目;
}
int stack_isEmpty(){
返回s.count==0;
}
char stack_topChar(){
返回s.stack[s.count-1];
}
//*******************************************
//**辅助操作
内等规数(字符c){
返回c>='A'&&c前缀TC){
堆栈推送(c);
打破
}
否则{
char cpop=stack_pop();
结果[idx++]=cpop;
}
}
}
}
else如果(c=='('){
堆栈推送(c);
}
如果(c=='),则为else){
char cpop=stack_pop();
while(cpop!='('){
结果[idx++]=cpop;
cpop=堆栈_pop();
}
}
}
而(!stack_isEmpty()){
char c=堆栈_pop();
结果[idx++]=c;
}
结果[idx]='\0';
printf(“%s”,结果);
}
//*******************************************
//**主要
int main(){
printf(“插入表达式:”);
字符*表达式;
字符c;
int-idx=0;
做{
c=getchar();
如果(c='\n'| | c==EOF)
c='\0';
表达式[idx++]=c;
}而(c!='\0');
toPostfix(表达式);
返回0;
}
我试着在谷歌上到处寻找解决方案,但没有找到,所以我希望你能在这里帮助我

我得到的错误可以在这里找到:

这个:

char *result;
将创建指向不确定位置的未初始化指针。它不会创建字符串。因此,使用
result[idx++]
访问它会调用未定义的行为

在您只想在本地使用字符串的情况下,最好创建一个固定大小的字符缓冲区:

char result[80];
您必须确保您的写入长度不会超过80个字符,并在字符串末尾为空字符留出空间


这只是解决眼前问题的一个快速方法。您应该研究数组、C字符串、指针和内存分配,以进一步了解这些东西在C中是如何工作的。

这是因为
char*expression;
不会为字符串分配任何内存,所以您不能只做
expression[idx++]=c;
之后。为什么您要编写自己的代码来读取一行,而不是使用
fgets()
getline()
?您在
toPostfix()
中的
char*result;
也有同样的问题。可以将它们声明为数组而不是指针,或者使用
malloc()
为它们分配内存并
free()
当你处理完它们时。@Blaze我只是试着这样做:
char*expression=malloc(strlen(expression)*sizeof(char))
但它仍然给我同样的错误。谢谢你这么快回复。