Please consider the following code.
enum type {CONS, ATOM, FUNC, LAMBDA};
typedef struct{
enum type type;
} object;
typedef struct {
enum type type;
object *car;
object *cdr;
} cons_object;
object *cons (object *first, object *second) {
cons_object *ptr = (cons_object *) malloc (sizeof (cons_object));
ptr->type = CONS;
ptr->car = first;
ptr->cdr = second;
return (object *) ptr;
}
In the cons function, variable ptr is of type cons_object*. But in the return value it is converted to type of object*.
- I am wondering how this is possible because
cons_objectandobjectare different structs. - Are there any issues in doing stuff like this?
Any thoughts!