I created an implementation of a Stack in C. Here are the relevant definitions/methods, I have stripped all the error checking/reallocation so don't comment on that:
typedef struct {
Element *data;
int top;
int size;
} Stack;
typedef void* Element;
void push(Stack *s, Element e) {
s->data[(s->top)++] = e;
}
Now in another method, I have a loop in which I call push(). Something like
int character;
Stack *s = createStack();
while((character = getchar()) != EOF) {
int tmp = character;
push(s, &tmp);
}
However, this doesn't function how I want it to. The stack receives the same address everytime, thus when each character is read, the "contents" of the entire stack change. How do I modify this to do what I want. e.g when abcde is read, the stack looks like (top-down) e,d,c,b,a.