Search code examples
cpostfix-mtainfix-notation

I am getting a uninitialized local variable error at line 210 and 135


I am getting a uninitialized local variable error on line 210 and 135 in this program for converting infix into postfix. Can you tell what I'm doing wrong?

/*
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;
}

I've tried looking everywhere on Google for solutions and have not found any so I was hoping you would help me here.

The error I am getting can be found here: https://i.sstatic.net/l1bQK.jpg


Solution

  • This:

    char *result;
    

    will create an uninitialized pointer that points to an indeterminate location. It does not create a string. Therefore, accessing it with result[idx++] invokes undefined behaviour.

    In your cases, where you just want to use the string locally, it is best to create a char buffer of a fixed size:

    char result[80];
    

    And you must make sure that you never write beyond the 80 characters, leaving space for the null character at the end of the string.

    That's only a quick fix for your immediate problem. You should look into arrays, C strings, pointers and memory allocation to learn more about how these things work in C.