Hello - I've written a queue and I'm trying to keep it generic as far as the data it could hold. For that purpose, my node structure is defined as such:
Code:
struct node {
void* data;
struct node* next;
}node;
The void* is because I don't want to restrict the data type in the node (int, char, struct). My problem is adding the data to the queue. In my enqueue method, I create a new node to add the data to:
Code:
void enqueue(queue* q, void* item)
{
//....
// Create a new node and assign data
node* new_node = (node*)malloc(sizeof(node));
new_node->data = malloc(sizeof(item));
memcpy(new_node->data, item,strlen((char *)item));
//...
}
My problem is using the memcpy function. I don't know how to copy exactly the size of the data type I intend to pass because of the generality of the code. In the above example I had to use strlen when I wanted to test it with a char*, but I would like to make it work for every data type without having to do a specific cast every time. I'd really appreciate help on this - thanks!