Function int(*Cmp)(void*,void*)

Asked

Viewed 418 times

1

I know it compares pointers and returns an integer that determines whether one is smaller than another, in order. But, when I will use it in main(), is giving some error. Can help me?

  • Enter your code so we can help. Preferably a [mcve].

  • int dllInsertAfterSpec(Dllist *l, void *data, void *key, int (*Cmp)(void *, void *));

  • This is the function. I want to know how to use it to compare data of a certain type, in main

  • Did the answer solve your problem? Do you think you can accept it now? See [tour] to understand how it works. It would be helpful to indicate to everyone that the solution was useful and satisfactory for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

The question does not give many details, but this is the signature of a function that will be received by the function parameter dllInsertAfterSpec().

This is an anonymous function. That is, it is a pointer to a code somewhere. This pointer is stored in some variable (or it can be passed straight as argument). You can pass any function, written in any language, as long as it has the signature to return an integer and pass two generic pointers as argument to it. Obviously the code is expected to use these arguments in a comparison and return a number indicating how it worked. Just viewing the documentation to know exactly how to write this function. A hypothetical example:

int FuncaoASerUsada(void *lhs, void *rhs) {
    unsigned int a = *((unsigned int*)lhs);
    unsigned int b = *((unsigned int*)rhs);
    return (b - a);
}
dllInsertAfterSpec(l, data, key, FuncaoASerUsada);

I put in the Github for future reference.

A example of use of pointers for functions can be found in this question. Has more example in this other.

Useful question to better understand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.