1

Given the following declaration for a function used to update a node in a doubly linked list:

uint8 dl_update(DL_LIST **node, uint16 new_tag, void *new_object,
    uint32 new_size, void (*destructor)(void*))

And the following call to that function:

dl_update(&dlt_list, _TAG, zulu, sizeof(*zulu),
    (void(*)(void*)) &_free_dlt_object(delta));

What about the last argument would cause the compiler to emit the error message "lvalue required as unary '&' operator"? The destructor function I am passing (and the structure it operates on) are defined as follows:

typedef struct DLT_OBJECT {
    char *word;
    uint8 number;
} DLT_OBJECT;

void _free_dlt_object(DLT_OBJECT *object) {
    free(object->word);
    free(object);
}

The following question seems to imply that its okay to cast a function pointer, so I do not believe that my doing so is causing the problem, although given that my code is running on a bare metal embedded system (listed as a possible gotcha in the question) I am not sure.

Is this a case of a simple grammatical error, or is it more of a structural problem? I have been operating under the impression that declaring function pointers with void pointer arguments was a good way of passing generic function pointers around to other functions...

Community
  • 1
  • 1
RBE
  • 175
  • 1
  • 11

1 Answers1

2

when using a function pointer, you are just passing a function, so you make the call like so

dl_update(&dlt_list, _TAG, zulu, sizeof(*zulu), _free_dlt_object);
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • I am confused (again) Keith. You are passing the function rather than a pointer to the function, and AFAIK they are not the same thing. And how can you omit the argument to the function pointer? I cannot reconcile your answer with the example code given in [this](http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work) post... – RBE Feb 19 '13 at 21:18
  • 1
    @RBE A function designator is in almost all contexts implicitly converted to a function pointer. And when you pass a function(-pointer) to a function, you mustn't write an argument there, since it's the function (pointer) you pass, not the result of a function call. – Daniel Fischer Feb 19 '13 at 21:24
  • @DanielFischer Does this mean that the `_free_dlt_object` function argument needs to be passed to `dl_update` separately to the pointer to `_free_dlt_object` itself? I understand what you are saying with regards to passing a _pointer_ to the function, but I don't understand how the pointed-to function receives its arguments. – RBE Feb 19 '13 at 21:36
  • 1
    @RBE The argument with which `_free_dlt_object` will be called in `dl_update` will be supplied by `dl_update`. That's the point of passing functions, so that the receiver can call them with whatever arguments it needs to pass. – Daniel Fischer Feb 19 '13 at 21:47
  • 1
    @RBE in this case, the function dl_update is basically saying, hey, as part of this update I'm going to have to delete some objects, but I don't know what needs to be done to free them up, so give me a function I can call when I want to delete an object. – Keith Nicholas Feb 19 '13 at 21:59
  • @DanielFischer Thanks for the clarification - now I understand how things are supposed to work! – RBE Feb 19 '13 at 22:00
  • 1
    @RBE: the syntax issues around function designators and function pointers are a little weird, but `_free_dlt_object` and `&_free_dlt_object` and `*_free_dlt_object` and even `*****_free_dlt_object` all evaluate to the same thing. – newacct Feb 20 '13 at 00:07