The following code crashes on the second Pop()
call. I'm a novice with C and I've been staring at this code now for over an hour and i can't see the error. Any ideas to help me out on why this code is crashing?
#include <stdio.h>
#define StackDataSize 100
typedef struct Stack
{
int index;
void *data[StackDataSize];
} Stack;
void* Pop(Stack *s)
{
if(s->index >= 0)
{
return s->data[s->index--];
}
else
{
fprintf(stderr, "ERROR: Stack Empty\n");
return NULL;
}
}
void Push(Stack *s, void *v)
{
if(s->index < StackDataSize)
{
s->data[++s->index] = v;
}
else
{
fprintf(stderr, "ERROR: Stack Full\n");
}
}
int main(void)
{
Stack s = {-1}, *intstack = &s;
int x = 123456;
Push(intstack, &x);
printf("%d\n", *(int*)Pop(intstack));
printf("%d\n", *(int*)Pop(intstack));
return 0;
}