-1
我有這樣的結構:如何用void *數據創建結構體?
struct node
{
int data;
struct node *next;
};
所以下面strcut有一個int數據,我希望函數接受CopyFunction這是baisicly指針接受無效*的函數,返回void *,我想我的函數獲取第一個節點中的數據副本連接到第二個數據副本f,現在我希望數據不僅是int我想在任何數據上使用此函數,所以我使用CopyFunction作爲指向函數的指針
struct node
{
Element data;
struct node *next;
};
typedef void* Element;
typedef Element (*copy_function) (Element);
struct node * concatLists(struct node *head1, struct node *head2, int cmp(struct node *),copy_function CopyFunction)
{
struct node *head = NULL;
struct node **current = &head;
for (; head1 != NULL; head1 = head1->next)
{
if (cmp(head1))
{
*current = malloc(sizeof(struct node));
(*current)->data = CopyFunction(head1->data);
(*current)->next = NULL;
current = &(*current)->next;
}
}
for (; head2 != NULL; head2 = head2->next)
{
if (cmp(head2))
{
*current = malloc(sizeof(struct node));
(*current)->data = CopyFunction(head2->data);
(*current)->next = NULL;
current = &(*current)->next;
}
}
:那我Saied如何接受void *的...我如何能在功能波紋管,如果我想改變結構是使用..例如10
,然後,如果我有一個INT結構,我可以只使用此功能和複印功能於這樣的INT:
static void* copyInt(void* num){
int* newInt=malloc(sizeof(*newInt));
*newInt=*(int*)num;
return newInt;
}
此外,我認爲這是更好的,如果我改變,並且不使用總是結構節點*我想有可能:typedef struct node_t * Node;
這看起來像一個[XY問題(http://xyproblem.info/) –
PS - 「name_t」形式的類型是保留的。 – ikegami