Ive been working at this problem for about a week, I have looked up plenty of “incompatible pointer type” warning solutions but I’m still confused as to how I can fix this compile error.
I get an error saying:
char_stack_interface.c: In function ‘pop_char’:
char_stack_interface.c:32: warning: passing argument 2 of ‘pop’ from incompatible pointer type
char_stack_interface.c: In function ‘top_char’:
char_stack_interface.c:43: warning: passing argument 2 of ‘top’ from incompatible pointer type
This is my code:
char_stack_interface.h:
#ifndef _CHAR_STACK_INTERFACE_H
#define _CHAR_STACK_INTERFACE_H
#include "stack.h"
extern status push_char(stack *p_s, char c);
extern status pop_char (stack *p_s, char *p_c);
extern status top_char (stack *p_s, char *p_c);
#endif
stack.h:
#ifndef _STACK_H
#define _STACK_H
#include "globals.h"
ABSTRACT_TYPE(stack);
extern status init_stack (stack *p_S);
extern bool empty_stack(stack *p_S);
extern status push (stack *p_S , generic_ptr data);
extern status pop (stack *p_S , generic_ptr *p_data);
extern status top (stack *p_S , generic_ptr *p_data);
#endif
char_stack_interface.c:
#include <stdio.h>
#include <stdlib.h>
#include "char_stack_interface.h"
#include "stack.h"
status push_char(stack *p_s, char c)
{
char *p_c = NULL;
p_c = (char *) malloc(sizeof(char));
if (p_c == NULL)
return ERROR;
*p_c = c;
if (push(p_s, p_c) == ERROR) {
free(p_c);
return ERROR;
}
return OK;
}
status pop_char (stack *p_s, char *p_c)
{
char *p_data;
if (pop(p_s, p_data) == ERROR)
return ERROR;
*p_c = *p_data;
free(p_data);
return OK;
}
status top_char (stack *p_s, char *p_c)
{
char *p_data;
if (top(p_s, &p_data) == ERROR)
return ERROR;
*p_c = *p_data;
return OK;
}
Well whatever the generic_ptr type is, obviously the compiler is not able to automatically cast your ‘char *’ type into generic ptr type. Try doing an explicit case of your second arg to pop and top e.g.:
pop(p_s, (generic_ptr)p_data)