top is a state variable for your stack, which is stored in a regular array. The variable top refers to the top of the stack, preserving the index of the array.
The first pop operation uses the decrement operator to change the top variable, and therefore the stack state: --top equivalent to top = top - 1 . The value is still in the array, but since the top variable now refers to a different index, this value is effectively deleted: the top of the stack is now another element. Now, if you call push , this popup value will be overwritten.
The second operation does not change the value of the top variable, it uses it only to return the value at the top of the stack. The top variable still refers to the same value as the top of the stack, and therefore the stack does not change.
source share