How to format a function pointer?

The only legal way to do this is to access the bytes making up the pointer using a character type. Like this: #include <stdio.h> int main() { int (*funcptr)() = main; unsigned char *p = (unsigned char *)&funcptr; size_t i; for (i = 0; i < sizeof funcptr; i++) { printf(“%02x “, p[i]); } putchar(‘\n’); … Read more

Why does having an `int (*)(float)` point to an `int foo()` trigger a warning, but having an `int (*)(double)` point to it doesn’t?

int foo() is declared without specifying its parameters. This is an obsolescent feature that lets you call it with any arguments. When calling the function, integer arguments are promoted to int (if needed), and float arguments are promoted to double. Due to this, it’s impossible for this function to receive a float parameter, which makes … Read more

Determining to which function a pointer is pointing in C?

You will have to check which of your 5 functions your pointer points to: if (func_ptr == my_function1) { puts(“func_ptr points to my_function1”); } else if (func_ptr == my_function2) { puts(“func_ptr points to my_function2”); } else if (func_ptr == my_function3) { puts(“func_ptr points to my_function3”); } … If this is a common pattern you need, … Read more

How does the template parameter of std::function work? (implementation)

After getting help from other answers and comments, and reading GCC source code and C++11 standard, I found that it is possible to parse a function type (its return type and its argument types) by using partial template specialization and function overloading. The following is a simple (and incomplete) example to implement something like std::function: … Read more

Do distinct functions have distinct addresses?

It looks like defect report 1400: Function pointer equality deals with this issue and seems to me to say that it is okay to do this optimization but as comments indicate, there is disagreement. It says (emphasis mine): According to 5.10 [expr.eq] paragraph 2, two function pointers only compare equal if they point to the … Read more

How to pass an argument to a function pointer parameter?

You can either use a lambda: repeat(lambda: bar(42)) Or functools.partial: from functools import partial repeat(partial(bar, 42)) Or pass the arguments separately: def repeat(times, f, *args): for _ in range(times): f(*args) This final style is quite common in the standard library and major Python tools. *args denotes a variable number of arguments, so you can use … Read more