Hello,

I came across a strange pointer (in my opinion): void *(*foo)(int *);
Why are so many stars there? It was explained like:

"...read inside-out; notice that the innermost element of the expression is *foo, and that otherwise it looks like a normal function declaration. *foo should refer to a function that returns a void * and takes an int *. Consequently, foo is a pointer to just such a function."

So I understood that foo is a pointer to a void function f(int). I tried to code this but failed with error: invalid conversion from 'void (*)(int*)' to 'void* (*)(int*)' [-fpermissive]

void aFunction(int*)
{
    cout << "something" << endl;
}

void f_voidFct()
{
    void *(*voidPtr)(int *);
    voidPtr=aFunction;  // here I get the error
    int a=5, *ptr_a=&a;
    voidPtr(ptr_a);
}

What do I miss here? Can anyone explain this *(*ptr)(type*) so I can get the idea?
Thanks!

Recommended Answers

All 7 Replies

The function is meant to return a void pointer. What are you returning?

... I'm stupid :P
It should be void *aFunction(int*) not void aFunction(int*). It seems to return a void pointer :)

Exactly what Moschops said. Here is a proper implementation, but still pretty useless... :-)

void* aFunction(int* pToInt)
{
    cout << "something" << endl;
    return (void*)pToInt;
}
void f_voidFct()
{
    void *(*voidPtr)(int *);
    voidPtr=aFunction;  // here I get the error
    int a=5, *ptr_a=&a;
    void* p = voidPtr(ptr_a);
    // p should have the same value as (void*)&a or (void*)ptr_a
}

I was just reading some tutorials and this stuff was there. I wanted to understand what was that about, but I'm not sure if it would be good for something.

Well, pointers to functions is useful stuff. It can be especially useful in complex polymorphic stuff. We use pointers to member functions for this sort of stuff, so you can change the behavior of classes dynamically at runtime. An example would be to enable different callback functions for event handlers depending upon differing criteria, without having to depend upon derived classes for each type. This is one of the more obscure areas of C++ programming, but I have found it useful in the past. That said, the last time I used pointer-to-member-functions was about 10 years ago. I generally try to keep stuff simpler than that since it tends to give one a headache trying to determine what is going on! Testing is a real PITA... :-)

Also, general pointers-to-functions is useful for what we call jump tables - functions to call based upon some index value. It is used a lot in Linux kernel programming - you get one input and you call one function. You get another input and you call something else. This can be done much more efficiently (and dynamically) than hard-coded if/else or switch statements.

Understood! As I do more C++, I get into more and more problems/headaches :)
Thanks for help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.