1

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;
}
8
  • 1
    insertNode->data = inArrayOfElements? I'm not sure about this Commented Oct 24, 2016 at 6:18
  • 2
    inserNode->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 only Element pointers, void* type requirement is rather strange one. Commented Oct 24, 2016 at 6:19
  • Yeah typecasting (void*)in this case is better, my bad. Commented Oct 24, 2016 at 6:21
  • thanks, I think its because each struc in the array will hold a different data type , so ArrayOfElements[1] could be a int and ArrayOfElements[0] could be a string Commented Oct 24, 2016 at 6:24
  • 1
    @RoadRunner actually, typecasting to 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 in inArrayOfElements as a single node in the list (and if so, where's the array size param?), or just a single node with data pointing to the base array pointer. Commented Oct 24, 2016 at 6:34

1 Answer 1

3

You can cast to a void pointer, like this:

inserNode->data = (void*)inArrayOfElements;
//                ^ Explicit cast here

This requirement makes a lot of sense if you'll be storing multiple types of data in your data field of the linked list.

Please note that in C, such a cast is optional and only for readability purposes. Conversion from Anything* to void* is strictly speaking unnecessary in C. In C++ it wouldn't, and there are downsides to it, as @WhozCraig points out in his comment. So pick your poison there.

Sign up to request clarification or add additional context in comments.

3 Comments

Conversion from AnyObjectType* to void* and back in C is automatic. No cast should be required.
Not certainly. It also makes it more brittle. If/when conditions change such as the param is no longer a basic pointer type the cast will happily (and unhelpfully) hide the mistake. With C++ it would be mandetory, but in C there is no need for it, and plenty of reasons to avoid it. Given a choice between using, and not using, an unnecessary cast, choose the latter.
@WhozCraig I added a disclaimer to the answer citing your comment. I hope its clearer now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.