I have an array of structs called arrayOfElements and I want them stored ( in better terms pointed to ) in a Linked List so I've malloc'd the arrayOfStrucs
arrayOfElements= malloc(4 * sizeof(Element));
and then once I have put in all the data in I want to pass it to a function called insert which imports the head of the list and the array of structs
LinkedList* insert(LinkedList* head, Element* inArrayOfElements)
My issue is that I've been told that the data member in the linked list has to be a void* , thats a hard requirement. So my question is, in the function insert how do I go about making void* data from the linked list
typedef struct LinkedList {
void* data
struct LinkedList* next;
} LinkedList;
point to the imported array of structs?
LinkedList* insert(LinkedList* head, Element* inArrayOfElements)
{
LinkedList* insertNode = malloc(sizeof(LinkedList));
insertNode->next = head;
/*WHAT DO I DO HERE TO MAKE void* data point to inArrayOfELements*/
return insertNode;
}
insertNode->data = inArrayOfElements? I'm not sure about thisinserNode->data = (void*)inArrayOfElements. This requirements makes sense, if you will use your linked list to store elements of different types. But then you will need some field to understand what data is in the node. If you store onlyElementpointers,void*type requirement is rather strange one.(void*)in this case is better, my bad.void*is pointless for this in C. Conversion is done automatically in both directions. Your initial command seems correct, though the question isn't exactly crystal clear. I can't tell whether the OP wants to store each element ininArrayOfElementsas a single node in the list (and if so, where's the array size param?), or just a single node withdatapointing to the base array pointer.