using free with a casted pointer..
hello everybody! I have one doubt.. Can free() be used with a casted pointer? My situation:
I have these 2 structs which implement a doubly-linked list:
Code:
struct list_head{
struct list_head *next;
struct list_head *prev;
};
typedef struct list_head ListHead;
typedef ListHead *ListHeadPtr;
struct node{
struct list_head lh;
int pid;
};
typedef struct node Node;
typedef Node *NodePtr;
and I have to implement my function to delete from list:
Code:
void delete_node (ListHeadPtr head, ListHeadPtr node)
{
(node->prev)->next=node->next;
(node->next)->prev=node->prev;
free((NodePtr)node);
}
is this ok? otherwise i could write the least general:
Code:
void delete_node (ListHeadPtr head, NodePtr node)
{
((node->lh).prev)->next = (node->lh).next;
((node->lh).next)->prev = (node->lh).prev;
free(node);
}
which one is better in your opinion?thanks!