Infintie loop problem

struct stack_type
{

int content;
struct stack_type *next;

};

typedef struct stack_top
{

struct stack_top *top;

}*Stack;

//Case 1:

void display(Stack s)//Works perfectly

{

struct stack_type *a;    
a=s->top;
while(a!=NULL)
{
    printf("%d ",a->content);
    a=a->next;
}

}

//Case 2:

void display(Stack s)
{

struct stack_type *a;
a=s->top;
while(s->top!=NULL)//Infinite loop
{
    printf("%d ",a->content);
    s->top=a->next;
}

}

Can anyone explain me why this is happening?

In the second function you are not changing the content of a. So it is always pointing to the same element. As a result you are in a infinite-loop.

1 Like

Thanks man. Silly me!

//