I wrote a Linked List ADT for a class I am in originally for a list of ints. Now I am going to use the same list for chars and ints. I know how to rewrite the code for chars to just have basically two List ADTs, one for the ints and the other for the chars.
I don't want to do that however, I want to write it for a generic variable so that in the future I can use this ADT with other code without worrying about types to much.
Initially I went into this with void*, but I am running into an error with it that I am just have a hard time understanding how to fix.
typedef struct NodePtr {
void* data;
struct NodePtr*next;
struct NodePtr*prev;
} NodePtr;
typedef struct ListStruct {
NodePtr* first;
NodePtr* last;
NodePtr* current;
} ListStruct;
Then some other code and fun stuff, and then my insert method to insert at the front of the list:
void insert(listhndl L, void* data)
{
...inserting of the node here.
}
This code throws no error, but then when I run a driver to test it:
ListHndl test = NULL;
test = newList();
insert(test, 1);
I get the error message:
warning: passing argument 2 of 'insert' makes pointer from integer without a cast [enabled by default]
insert(test, 1);
^
error: expected 'void *' but argument is of type 'int'
void insert(ListHndl L, void* data);
I am confused here because how can it throw an error saying it's not the type it expected, if void* is a generic type?
What am I doing wrong?
I saw on here some people recommend using enum and unions for generics instead of void*, but I could not get that to work ether as I don't really understand what to do with them. If somebody wanted to also answer how to do generics with the enums/unions method I would greatly appreciate it.
void*is a 'generic' pointer type. It still has to be some pointer.uintptr_tperhaps