I'm pretty new to C so I decided to implement SRFI-1 (linked-list library) to help me learn.
What I have so far is working, but only handles integers.
typedef struct pair {
int car;
struct pair *cdr;
} pair;
From what I've read I can use a void * for the car:
typedef struct pair {
void *car;
struct pair *cdr;
} pair;
This works but I get lots of these...
llist.c:262:3: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘void *’
llist.c:27:7: note: expected ‘void *’ but argument is of type ‘int’
I've found some workarounds but before diving in and fixing everything I want to be sure this is the appropriate way to go.
The only other way I can think of is using some sort of generic object type:
typedef struct object {
union {
int fixnum;
double flonum;
}
}
typedef struct pair {
object *car;
struct pair *cdr;
} pair;
Or something similar.
Is there a best practice for generic functions/data types?
The full source is here.