Linked lists as containers

Posted By: carlpa

Linked lists as containers - 12/08/10 16:55

To all the wonderful, kind, and understanding programers out there,

Thank you in advance for your help. I am trying to create a linked list set of "methods" which can be in a "linked_list.c" file and included in many different programs. My idea is create a list of elements. Each element has a void pointer (void* ptr;) as a member. This pointer should be able to point to anything, structure, variable, STRING*, etc, which is why it is declared void in the element definition.

What is the best way to assign this pointer to the required item?
Posted By: WretchedSid

Re: Linked lists as containers - 12/08/10 17:04

"What is the best way to assign this pointer to the required item?"
By casting it to a void pointer. Srsly, all pointers are equal in their bitdepth and endianess (at least throughout one application run), no matter of their name. Its just to make it more convenient for the user, but in fact you could do everything with a typeless void pointer as well.
Posted By: carlpa

Re: Linked lists as containers - 12/08/10 18:15

Thank you.

I assume that your reply is to be used thusly:

Element.ptr = (void*) pointer-to-item

not (void) even though the variable pointer-to-item is already a pointer and Element.ptr is defined as void*.

Using Element.ptr = pointer-to-item does not seem to work.
© 2024 lite-C Forums