patterncMinor
Generic linked-list library in C
Viewed 0 times
genericlistlinkedlibrary
Problem
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.
From what I've read I can use a void * for the car:
This works but I get lots of these...
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:
Or something similar.
Is there a best practice for generic functions/data types?
The full source is here.
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.
Solution
void is the standard to implement such generics in C. The trouble with union is that the end-user cannot put their own types/structs in it. With void a pointer to a user-defined struct can be used. However, be careful about memory leaks!
The warnings you are getting is because you haven't consistently used void*.
This indicates that you are printf'ing the data as if it were an int. But you don't what it is so you can't do that. Either don't provide printing options or provide a way to control how the object prints.
You have a void and you are passing it to a function that takes int. Everything inside your library should be using void not int. Only the actual code using your library should conver the void * into int or whatever they are using.
The warnings you are getting is because you haven't consistently used void*.
llist.c:262:3: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘void *’This indicates that you are printf'ing the data as if it were an int. But you don't what it is so you can't do that. Either don't provide printing options or provide a way to control how the object prints.
llist.c:27:7: note: expected ‘void *’ but argument is of type ‘int’You have a void and you are passing it to a function that takes int. Everything inside your library should be using void not int. Only the actual code using your library should conver the void * into int or whatever they are using.
Code Snippets
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’Context
StackExchange Code Review Q#2564, answer score: 3
Revisions (0)
No revisions yet.