Reputation: 49
The program is supposed to implement stack for storing and retrieving struct pointers. The struct contains one int and two struct variables. Push function is working fine but when i pop the struct pointer and try to access data in it there is an execution error.
#include<stdio.h>
#include<malloc.h>
#define MAX 10
struct node *arr[MAX];
int top=-1;
struct node* pop(){
if(top=-1)
return NULL;
else{
return arr[top--];
}
}
void push(struct node* d){
if(top==MAX-1)
return;
else{
arr[++top]=d;
}
}
int main(){
struct node* t = (struct node*)malloc(sizeof(struct node));
t->data=9;
push(t);
printf("%d",pop()->data);
return 0;
}
Upvotes: 2
Views: 644
Reputation: 7006
if( top = -1)
should be
if( top == -1 )
With =
you are assigning -1
to top
. To check for equality, use ==
.
Upvotes: 7