I implement a simple priority queue in C for the kernel, so I cannot use standard libraries. The queue contains the head of the node, and each node points to the next in the queue.
typedef struct node node; struct node { node *next; void *data; }; typedef struct { node *head; int n; } queue;
As you can see, each node stores data in a void *. I am having trouble converting this data to say int when I pop the data from the stack.
//push data int int_data = 100; push(q, &int_data); //... //pop data node* popped = pop(q); int *pop_data = popped->data; printf("pop data (100): %d\n", *pop_data);
Why can't I get the original value here? I seem to be typing the value of a pointer. Alternatively, is there a better way to handle this?
== edit (sorry, should have included them):
void push(queue *q, void *data) { node new; new.data = data; node *new_ptr = &new; if(is_empty(q)) { q->head = new_ptr; q->n++; return; } int i; node *curr = q->head; for(i=0; i<q->n; i++) { curr = curr->next; } curr->next = new_ptr; q->n++; } node* pop(queue *q) { node *curr = q->head; q->head = curr->next; return curr; }
source share