简体   繁体   中英

C: Stack Dump error for infix to postfix conversion

I have written the following code for converting infix expression to postfix,but a run-time error is displayed.

The error is as follows:

Input string: a+bc*d 1 [main] infix_to_postfix 7340 cygwin_exception::open_stackdumpfile: Dumping stack trace to infix_to_postfix.exe.stackdump

Following is my code:

#include<stdio.h>
#include<string.h>
#define size 20

struct stack{
    int a[size],top;
    int temp[size], tos;
}s;

// Push operation....
void push(struct stack s,int item){
    if(s.top >= size-1){
        printf("\nStack overflow..\n");
    }
    else{
        s.a[++s.top] = item;
    }
}

// Pop operation....
int pop(struct stack s){
    if(s.top == -1){
        printf("\n..Stack underflow..\n");
}
else {
    return s.a[s.top--];
}
}


// function f starts from here. f returns the precedence value of corresponding symbol.
int f(char symbol){
    if (symbol == '+' || '-')
        return 1;
    if (symbol == '*' || '/')
        return 2;
    if (symbol == '#')
        return 0;
    char c;
for(c='a'; c<='z'; c++){
    if(symbol == c)
        return 3;
}
}

int main(){
s.top = -1;
s.a[++s.top] = '#';

int i = 0;
char input[20], polish[21];
char next, temp;

printf("Input string: ");
scanf("%s", input);
strcat(input, '#');
next = input[i++];

while(next != '\0'){
    while(f(next) > f(s.a[s.top])){
        push(s, next);
        next = input[i++];
    }
    while(f(next) <= f(s.a[s.top])){
        temp = pop(s);
        strcat(polish, temp);
        printf("\nPolish: %s", polish);
    }
}


return 0;
}
strcat(polish, temp);

polish is never initialized so using uninitialized array leads to undefined behavior. strcat() expects a null-terminated string.

Try doing something like

char polish[21] = "";

Edits:

As @BLUEPIXY points out strcat() expects char * not char

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM