Below is the program written in C.
#include<stdio.h>
#include<stdlib.h>
struct stack{
int data;
struct stack *next;
};
void push(struct stack *t,int d){
struct stack *nptr;
nptr=(struct stack*)malloc(sizeof(struct stack));
nptr->data=d;
nptr->next=t;
t=nptr;
}
void display(struct stack *h){
while(h!=NULL){
printf("%d\n",h->data);
h=h->next;
}
}
void pop(struct stack *t){
struct stack *temp=t;
t=t->next;
free(temp);
}
int main(){
struct stack *top=NULL;
push(top,5);
push(top,6);
push(top,7);
display(top);
pop(top);
display(top);
return 0;
}
When the function is passed with pointer, as shown in the program the output is:"shell returned-1073741819".
When function is passed without parameters by declaring top as global variable the code runs fine. I would like to know what mistake I have done in the program. Please help me out.