I built a stack data structure, which has a peek method. The compiler is giving me a warning: initialization makes pointer from integer without a cast warning when calling peek in main. Here is what I have:
stack.h:
struct stack_elem {
struct stack_elem *next;
};
struct stack {
struct stack_elem *top;
};
void stack_init(struct stack *stack);
int isEmpty(struct stack *);
struct stack_elem * peak(struct stack *);
And its implementation:
void stack_init(struct stack *stack) {
stack->top = NULL;
}
int isEmpty(struct stack *stack) {
if (stack->top == NULL)
return 1;
else
return 0;
}
struct stack_elem * peek(struct stack *stack) {
if (isEmpty(stack) == 1)
return NULL;
else
return stack->top;
}
In my main.c, I define stack as follows:
struct stack stack;
stack_init(&stack);
and call peek:
struct stack_elem * elem = peek(&stack);
This line throws the warning. The weird thing here is that I used to import stack.c into main.c and just compile with $ gcc main.c. This had absolutely no errors. Now I changed the import to stack.h, and I am compiling with $ gcc main.c stack.c which throws the warnings. Running it also segfaults (which it didn’t beforehand).
Change:
To:
: )