Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

In the given C snippet, find the statement number that has error.
//C code to push an element into a stack
void push( struct stack *s, int x)
{
if(s->top==MAX-1)
{
printf(“stack overflow”);
}
else
{
s->items[++s->top]=x;
s++;
}
}

In the given C snippet, find the statement number that has error.
//C code to push an element into a stack
void push( struct stack *s, int x)
{
if(s->top==MAX-1)
{
printf(“stack overflow”);
}
else
{
s->items[++s->top]=x;
s++;
}
}
a) 1
b) 9
c) 10
d) 11

Answer: c
Explanation: If the stack is not full then we are correctly incrementing the top of the stack by doing “++s->top” and storing the value of x in it. However, in the next statement “s++”, we are un-necessarily incrementing the stack base pointer which will lead to memory corruption during the next push() operation.

Join The Discussion