In brief, I am attempting to use a void pointer as a parameter to a function pointer, but am getting the compiler error "invalid use of void expression".
I have a doubly linked list (DLL) whose node structure is as follows:
typedef struct DL_LIST
{
uint16 tag; /* Object ID tag */
struct DL_LIST *previous;
struct DL_LIST *next;
void *object; /* A pointer to this node's object */
uint32 size; /* The size of this node's object, in bytes */
} DL_LIST;
I also have the following function which is used to delete a single such node:
void dl_delete(DL_LIST *node, void (*dl_destructor)(void*)) {
if (node != NULL) {
dl_extract(node); /* Removes the node from the list */
if (node->object != NULL) {
(*dl_destructor)(node->object);
free(node->object);
}
free(node);
}
}
where the node extraction function is:
DL_LIST *dl_extract(DL_LIST *node) {
if (node != NULL) {
if (node->previous != NULL) {
node->previous->next = node->next;
}
if (node->next != NULL) {
node->next->previous = node->previous;
}
node->previous = NULL;
node->next = NULL;
}
return node;
}
The idea here is to be able to pass a separate destructor function for each type of object
that may be stored in a node. This destructor function takes a pointer to the object as a parameter, and is used to free any heap memory that is being used by children of the object
.
The aforementioned error occurs when I try to call dl_delete()
from a function designed to delete an entire DLL:
void dl_destroy(DL_LIST **list, void (*dl_destructor)(void*)) {
DL_LIST *marker;
DL_LIST *previous_node;
if (*list != NULL) {
previous_node = (*list)->previous;
while (previous_node != NULL) {
marker = previous_node->previous;
dl_delete(previous_node, (*dl_destructor)(previous_node->object));
previous_node = marker;
}
/* Code removed for brevity */
}
}
I have read this introduction to function pointers, but am still unable to determine how to remedy the problem. An explanation of what I am doing wrong would be most appreciated.