I have here a function get_stack
that instantiates a stack and should return a pointer to the stack, but doesn't. Still, the program continues and updates the stack correctly.
#include <stdio.h>
#include <stdlib.h>
#define STACKLIM 1000
struct stack {
char data[STACKLIM];
int top;
};
typedef struct stack* Stack;
Stack get_stack() {
Stack s = (Stack)(malloc(sizeof(struct stack)));
s->top = -1;
//return s;
}
void push(Stack s, char val) {
if(s->top == STACKLIM) {
printf("ERROR: Stack Overflow\n");
}
else {
s->top += 1;
s->data[s->top] = val;
}
}
void display(Stack s) {
int i;
printf("Stack -> ");
for(i = 0; i <= s->top; i++) {
printf("%c ", s->data[i]);
}
printf("\n");
}
int main() {
Stack d = NULL;
d = get_stack();
push(d, 'a');
display(d);
return 0;
}
It's like the return statement does not matter. What could be the reason for this? I am using gcc 4.5.2 on an RH5 machine.