0
  while (!isEmpty(s))
    {
        *c = pop(s);
        strcat(postfix, c);
        strcat(postfix, " ");
    }

This is where the crash happens specifically at the *c line. When i display the stack it has a value of 43.00000 which means it contains the '+' in ASCI code. I also used these 3 lines above this specific one in two loops which gave no error.

float pop(stack *s)
{
    float x = s->top->data;
    node *temp = s->top;
    s->top = s->top->next;
    free(temp);
    return x;
}

Here is the pop function. Also this works in vscode and the crashes happen in codeblocks only

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
typedef struct node
{
    float data;
    struct node *next;
} node;
typedef struct
{
    node *top;
} stack;
node *newNode(float x)
{
    node *n = malloc(sizeof(node));
    n->data = x;
    n->next = NULL;
    return n;
}
stack *init()
{
    stack *s = malloc(sizeof(stack));
    s->top = NULL;
    return s;
}
float pop(stack *s)
{
    float x = s->top->data;
    node *temp = s->top;
    s->top = s->top->next;
    free(temp);
    return x;
}
void push(stack *s, float x)
{
    node *n = newNode(x);
    if (s->top == NULL)
        s->top = n;
    else
    {
        n->next = s->top;
        s->top = n;
    }
}
float peek(stack *s)
{
    float x = s->top->data;
    return x;
}
int isEmpty(stack *s)
{
    return (s->top == NULL);
}
void display(stack *s)
{
    stack *temp = init();
    while (!isEmpty(s))
        push(temp, pop(s));
    while (!isEmpty(temp))
    {
        printf("%f ", peek(temp));
        push(s, pop(temp));
    }
    printf("\n");
}
int priority(char op)
{
    if (op == '+' || op == '-')
        return 1;
    if (op == '*' || op == '/')
        return 2;
    if (op == '^')
        return 3;
    else
        return 0;
}
int isOperator(char op[5])
{
    if (!strcmp(op, "+") || !strcmp(op, "-") || !strcmp(op, "*") || !strcmp(op, "/") || !strcmp(op, "^") || !strcmp(op, "(") || !strcmp(op, ")"))
        return 1;
    else
        return 0;
}
int isDigit(char digit[20])
{
    int i = 0;
    if (digit[0] == '-')
        i++;
    for (i; digit[i] != '\0'; i++)
        if (!isdigit(digit[i]) && digit[i] != '.')
            return 0;
    return 1;
}
char *infixToPostfix(char *infix)
{
    char postfix[100] = "", tokens[20][2000], *temptok, *c;
    int i = 0;
    temptok = strtok(infix, " ");
    strcpy(tokens[i++], temptok);
    while (temptok != NULL)
    {
        temptok = strtok(NULL, " ");
        if (temptok != NULL)
            strcpy(tokens[i++], temptok);
    }
    int ntok = i;
    stack *s = init();
    for (i = 0; i < ntok; i++)
    {
        if (isDigit(tokens[i]) && strcmp(tokens[i], "-"))
        {
            strcat(postfix, tokens[i]);
            strcat(postfix, " ");
        }
        else if (isOperator(tokens[i]))
        {
            if (isEmpty(s))
                push(s, tokens[i][0]);
            else if (tokens[i][0] == '(')
                push(s, tokens[i][0]);
            else if (tokens[i][0] == ')')
            {
                while (peek(s) != '(')
                {
                    *c = pop(s);
                    strcat(postfix, c);
                    strcat(postfix, " ");
                }
                pop(s);
            }
            else
            {
                while (!isEmpty(s) && priority(peek(s)) >= priority(tokens[i][0]) && tokens[i][0] != '(')
                {
                    *c = pop(s);
                    strcat(postfix, c);
                    strcat(postfix, " ");
                }
                push(s, tokens[i][0]);
            }
        }
    }
    display(s);
    system("pause");
    while (!isEmpty(s))
    {
        *c = pop(s);
        strcat(postfix, c);
        strcat(postfix, " ");
    }
    strcpy(infix, postfix);
    return infix;
}
int main()
{
    char infixExpr[256] = "";
    printf("Enter an expression you want to evaluate or Ctrl+Z to exit: ");
    while (fgets(infixExpr, 255, stdin) != NULL)
    {
        replaceNewLineBySpace(infixExpr);
        char *postfix = infixToPostfix(infixExpr);
        printf("Postfix : %s\n", postfix);
        float result = evaluatePostfix(postfix);
        printf("Result: %f\n", result);
        system("pause");
        // exit(1);
    }
    return 0;
}
Ossama
  • 11
  • 2
  • It is not productive to try and debug incomplete code snippets. Please provide complete code as a [mre]. Please review [ask]. – kaylum Apr 08 '22 at 21:29
  • Should i put the whole code? – Ossama Apr 08 '22 at 21:29
  • Not the whole code. Please read the link. Remove all code that is not needed to reproduce the problem. For example, replace user input with a fixed string. Remove operations on the data and just do `pop` sequence to try and reproduce the issue. – kaylum Apr 08 '22 at 21:31
  • i did my best to upload where i think it originates – Ossama Apr 08 '22 at 21:34
  • 5
    You never initialized `c`. Where is `*c` supposed to be assigning? – Barmar Apr 08 '22 at 21:34
  • Please elaborate – Ossama Apr 08 '22 at 21:35
  • Aside: `s->top = s->top->next;` in `pop()` does not seem right as you're freeing `s->top` (a.k.a. `temp`) right after. – 500 - Internal Server Error Apr 08 '22 at 21:36
  • 3
    "*Please elaborate*". `char *c;` declares an uninitialised pointer that doesn't have a valid memory address. So when you dereference it with `*c` you are accessing invalid memory. – kaylum Apr 08 '22 at 21:39
  • 1
    It's unclear what you are trying to do at all. You pop a `float`, store it in a `char` and try to use it as a `char *`. You need to pop as a `float` and use it as a `float`. If you want to add it to a string it needs to be converted into a string, for example using `snprintf`. – kaylum Apr 08 '22 at 21:41
  • 1
    Ossma, Save time, enable all warnings. – chux - Reinstate Monica Apr 08 '22 at 22:14
  • [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/995714) – phuclv Apr 09 '22 at 01:22

0 Answers0