Questions tagged [function-pointers]

A function pointer is a pointer to a function, which can be stored in a variable. It allows a run-time choice of which function to run.

In programming languages like C, function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.

Many "pure" object-oriented languages do not support function pointers. Something similar can be implemented in these kinds of languages, though, using references to interfaces that define a single member function. Microsoft .NET languages such as C# and Visual Basic .NET implement type-safe function pointers with delegates.

In other languages that support first-class functions, functions are regarded as data, and can be passed, returned, and created dynamically directly by other functions, eliminating the need for function pointers.

(Source: Wikipedia)

Popular Questions

4363 questions
66
votes
4 answers

What's the syntax for declaring an array of function pointers without using a separate typedef?

Arrays of function pointers can be created like so: typedef void(*FunctionPointer)(); FunctionPointer functionPointers[] = {/* Stuff here */}; What is the syntax for creating a function pointer array without using the typedef?
Maxpm
  • 24,113
  • 33
  • 111
  • 170
66
votes
13 answers

How to get function's name from function's pointer in Linux kernel?

How to get function's name from function's pointer in C? Edit: The real case is: I'm writing a linux kernel module and I'm calling kernel functions. Some of these functions are pointers and I want to inspect the code of that function in the kernel…
Daniel Silveira
  • 41,125
  • 36
  • 100
  • 121
63
votes
3 answers

Function pointer as parameter

I try to call a function which passed as function pointer with no argument, but I can't make it work. void *disconnectFunc; void D::setDisconnectFunc(void (*func)){ disconnectFunc = func; } void D::disconnected(){ *disconnectFunc; …
Roland Soós
  • 3,125
  • 4
  • 36
  • 49
62
votes
3 answers

Are functors actually faster than pointers to functions?

According to Scott Meyers, one area where C++ shines over C is that function objects are faster than function pointers. He says this is because function objects are inlined, which increases speed. I have two questions about this: How can we verify…
user7140484
  • 920
  • 6
  • 14
61
votes
4 answers

What does ((void (*)())buf)(); mean?

I am solving a binary exploitation challenge on picoCTF and came across this piece of code: ((void (*)())buf)(); where buf is a character array. I solved the challenge but can't seem to understand what exactly it's doing. I looked at this thread…
sh.3.ll
  • 815
  • 7
  • 17
59
votes
4 answers

Do distinct functions have distinct addresses?

Consider these two functions: void foo() {} void bar() {} is it guaranteed that &foo != &bar? Similarly, template void foo() { } is it guaranteed that &foo != &foo? There are two linkers I know of that fold function…
59
votes
11 answers

Javascript Function-Pointer Assignment

Consider this javascript code: var bar = function () { alert("A"); } var foo = bar; bar = function () { alert("B"); }; foo(); When running this code I get "A". Is this behavior a part of javascript specification and can I rely on it?
niaher
  • 9,460
  • 7
  • 67
  • 86
58
votes
5 answers

Function pointers and address of a function

So I figured when making function pointers, you do not need the operator & to get the address of the initial function: #include double foo (double x){ return x*x; } int main () { double (*fun1)(double) = &foo; double…
mmirzadeh
  • 6,893
  • 8
  • 36
  • 47
58
votes
6 answers

Comparing std::functions for equality?

How can I compare two C++11 std::functions with operator==, and return true if both of said functions refer to the same function pointer?
JesseTG
  • 2,025
  • 1
  • 24
  • 48
58
votes
3 answers

How to pass an argument to a function pointer parameter?

I only just started learning Python and found out that I can pass a function as the parameter of another function. Now if I call foo(bar()) it will not pass as a function pointer but the return value of the used function. Calling foo(bar) will pass…
Byzantian
  • 3,208
  • 4
  • 27
  • 31
57
votes
5 answers

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

In Bjarne Stroustrup's home page (C++11 FAQ): struct X { int foo(int); }; std::function f; f = &X::foo; //pointer to member X x; int v = f(&x, 5); //call X::foo() for x with 5 How does it work? How does std::function call a foo…
Sadeq
  • 7,795
  • 4
  • 34
  • 45
55
votes
5 answers

How define an array of function pointers in C

I've a little question. I'm trying to define an array of function pointers dynamically with calloc. But I don't know how to write the syntax. Thanks a lot.
BEPP1
  • 945
  • 2
  • 11
  • 15
54
votes
9 answers

Determining to which function a pointer is pointing in C?

I have a pointer to function, assume any signature. And I have 5 different functions with same signature. At run time one of them gets assigned to the pointer, and that function is called. Without inserting any print statement in those functions,…
Sumit
  • 1,485
  • 11
  • 24
54
votes
6 answers

How to format a function pointer?

Is there any way to print a pointer to a function in ANSI C? Of course this means you have to cast the function pointer to void pointer, but it appears that's not possible?? #include int main() { int (*funcptr)() = main; …
53
votes
1 answer

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

I have this piece of code: int foo() { return 0; } int main() { int (*float_function)(float) = foo; } When compiled using x86-64 GCC 12.2, with -Wall, it produces the warning (Link): warning: initialization of 'int (*)(float)' from…