简体   繁体   English

将void指针传递给函数指针

[英]Passing a void pointer to a function pointer

In brief, I am attempting to use a void pointer as a parameter to a function pointer, but am getting the compiler error "invalid use of void expression". 简而言之,我试图将void指针用作函数指针的参数,但是却遇到了编译器错误“无效使用void表达式”。

I have a doubly linked list (DLL) whose node structure is as follows: 我有一个双链表(DLL),其节点结构如下:

typedef struct DL_LIST
{
    uint16 tag;                 /* Object ID tag */
    struct DL_LIST *previous;
    struct DL_LIST *next;
    void *object;               /* A pointer to this node's object */
    uint32 size;                /* The size of this node's object, in bytes */
} DL_LIST;

I also have the following function which is used to delete a single such node: 我还具有以下用于删除单个此类节点的功能:

void dl_delete(DL_LIST *node, void (*dl_destructor)(void*)) {
    if (node != NULL) {
        dl_extract(node);       /* Removes the node from the list */

        if (node->object != NULL) {
            (*dl_destructor)(node->object);

            free(node->object);
        }

        free(node);
    }
}

where the node extraction function is: 节点提取函数为:

DL_LIST *dl_extract(DL_LIST *node) {
    if (node != NULL) {
        if (node->previous != NULL) {
            node->previous->next = node->next;
        }

        if (node->next != NULL) {
            node->next->previous = node->previous;
        }

        node->previous = NULL;
        node->next = NULL;
    }

    return node;
}

The idea here is to be able to pass a separate destructor function for each type of object that may be stored in a node. 这里的想法是能够为可能存储在节点中的每种object传递单独的析构函数。 This destructor function takes a pointer to the object as a parameter, and is used to free any heap memory that is being used by children of the object . 此析构函数将指向对象的指针作为参数,并用于释放object子级正在使用的所有堆内存。

The aforementioned error occurs when I try to call dl_delete() from a function designed to delete an entire DLL: 当我尝试从旨在删除整个DLL的函数中调用dl_delete()时,会发生上述错误:

void dl_destroy(DL_LIST **list, void (*dl_destructor)(void*)) {
    DL_LIST *marker;
    DL_LIST *previous_node;

    if (*list != NULL) {
        previous_node = (*list)->previous;

        while (previous_node != NULL) {
            marker = previous_node->previous;
            dl_delete(previous_node, (*dl_destructor)(previous_node->object));
            previous_node = marker;
        }

        /* Code removed for brevity */
    }
}

I have read this introduction to function pointers, but am still unable to determine how to remedy the problem. 我看了这个介绍函数指针,但我仍然无法确定如何解决问题。 An explanation of what I am doing wrong would be most appreciated. 对于我在做什么错的解释,将不胜感激。

this line 这条线

dl_delete(previous_node, (*dl_destructor)(previous_node->object));

needs to be dl_delete(previous_node, dl_destructor); 需要是dl_delete(previous_node, dl_destructor);

also in dl_delete this line (*dl_destructor)(node->object); 也在dl_delete这行(*dl_destructor)(node->object);

should be dl_destructor(node->object); 应该是dl_destructor(node->object);

also, just for safety, I like to check that my function pointers are not null before trying to make a call using them 另外,为了安全起见,我想在尝试使用函数指针进行调用之前检查其函数指针是否不为null

so in dl_delete something like :- 所以在dl_delete中类似:-

if(dl_destructor!=NULL) dl_destructor(node->object);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM