Board index » cppbuilder » Function pointers

Function pointers

Hi,

How can I use function pointer.

I'm trying to use a function defined in exe in Dll.

Thanks

 

Re:Function pointers


"Sel?uk Aryt" <sa...@ixir.com>:

Quote
> Hi,

> How can I use function pointer.

> I'm trying to use a function defined in exe in Dll.

The declaration syntax for function pointers is a little weird (looks a bit
lisp like). Anyway, here it goes.

As you know,

int foo(int);

declares a function called foo that takes one int argument and returns an
int value.

int (*fp)(int);

declares a pointer to a function called fp that takes one int argument and
returns an int value.

Function pointers can be used like this:

#include <iostream>

int foo(int n) {
    std::cout << "foo called with n = " << n << std::endl;

Quote
}

int main() {
    int (*fp)(int) = &foo; // assigns address of foo to fp; the ampersand
(&) can be omitted

    fp(42); // calls foo(42);

Quote
}

sm

Other Threads