Find the error (if any) in the following code snippet for pop operation. [code] void pop() //removing an element from a stack { printf(“%s”

Find the error (if any) in the following code snippet for pop operation.
Code:
void pop() //removing an element from a stack
{
     printf(“%s”, stack[top++]);
}
A. run time error
B. compile time error
C. pop operation is performed, but top moved in wrong direction
D. pop operation is performed properly
 
Answer: C
Explanation: The statement printf(“%s”, stack[top++]) does a pop, but top gets incremented which is not correct. The statement stack[top++] should be replaced with stack[top–] in order to pop an operand and maintain stack properly.
 

Tìm kiếm

Back
Top