I am not too experienced in C and I am having trouble with something that might be simple you for most of you. Basically, I have this structure that defines a 'generic' queue with a resizing array implementation:
typedef void (*free_fptr)(void *);
typedef struct {
void **queue; // pointer to generic type
size_t first; // head index in array
size_t last; // tail index in array
size_t size; // number of elements
size_t capacity; // capacity of array
size_t elem_size; // size in bytes of each element in queue
free_fptr deleter; // function used to free each element
} Queue;
Now, I have a data type that I want to put in the queue :
typedef struct {
Process_state state;
Queue time_queue;
unsigned int start_time;
unsigned int id;
} Process;
I also have a function 'Queue_destroy(Queue *q)' that I want to call when I need to free each element in the queue :
void
Queue_destroy(Queue *q)
{
size_t i;
for (i = 0; i < q->size; ++i) {
q->deleter(q->queue[(q->first + i) % q->capacity]);
}
free(q->queue);
}
Now, my problem is that I don't know to access to the 'Process' queue inside the queue from a void pointer. For example :
void
Process_deleter(void *item)
{
// Here I want to access the queue inside (Process *)item
free((Process *)item);
}
I tried many things without success such as :
Queue_destroy((*(Process *)item).time_queue);
Queue_destroy((Process *)item->time_queue);
It does not compile and I am clueless!
Any help would be greatly appreciated!