0
typedef struct _st {
    char Symbol;
    struct _st* next;
}st;
st* stackTop = NULL;

void push(st* stackTop, char symbol);

int main(){
if (isOpenSymbol(expression[index])) {
            push(stackTop, expression[index]);
            symbolCount++;
   }
}

void push(st* stackTop, char symbol) {
    st* newNode = (st*)malloc(sizeof(st));
    newNode->Symbol = symbol;
    newNode->next = stackTop;
    stackTop = newNode;
}

I'm trying to make checking balanced symbol program by using stack. This is part of the code. Push function creates newnode and stackTop points it. But when the function is finished the stackTop is initialized pointing NULL.

I tried that the function return newnode's address so stackTop point newnode like this.

stackTop = push(stackTop, expression[index].

When the push function is finished stackTop points newnode.

What is the difference between these and what am I missing?

  • 1
    `stackTop` is local to the function. Any change you make to it does not reflect on the original variable. Try `void push(st** stackTop, char symbol);` to pass the address of the pointer (and what ever else you need to change). – pmg May 02 '22 at 10:08

0 Answers0