trying to build generic linked list- help

Hi
i started to write a code of generic linked list in c
but i having trouble
my struct looking like :
1
2
3
4
typedef struct Node{
	void* item;
	struct Node* next;
}Node;


and i've wrote a function to create list :

1
2
3
4
5
6
Node* createNode(void* data){
	Node* newNode = (Node*)calloc(1,sizeof(Node));
	newNode->item = data;
	newNode->next = NULL;
	return newNode;
}


and this is the main program

1
2
3
4
5
6
int main(int argc, char** argv){
	int *ptr,num=5;
	ptr = #
	createNode(&num);
	return 0;
}


my problem is that i can't understand how to send to the function that gets void*, a int type
i tried in many ways and not succes
hope you can help me
The code you have posted is valid C. I don't understand what the problem is.
Why are you attempting to use a void data type in your function, let alone for your struct member "item"? Are you trying to be able to pass multiple data types to the "item" member? If not, there's no reason why you can't make "void item" into "int item". If so, you'll most likely need to use a template.

Remember, the whole idea behind a linked list is that you're creating pointers to dynamic objects of your struct. When the first dynamic object of Node is created along with all of the information contained therein (in this case just "item"), and you're left with a pointer, this becomes the head and tail of your linked list. When the second dynamic object is created, the pointer within the first object should point to the second object, and the second object will become the tail. Remember though, the data held within your struct doesn't have to be a pointer (just the "next" member), so unless you are truly wanting to also create and store pointers within your struct itself, there's no reason to use "void *item" or "int* item", as "int item" will do just fine.

If that helps at all, try modifying your code and we'll see where you stand.
my assignment is to build a generic linked list because of that i'm using void*, i don't know what objects will be in the list.
Topic archived. No new replies allowed.