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
1481
votes
12 answers

How do function pointers in C work?

I had some experience lately with function pointers in C. So going on with the tradition of answering your own questions, I decided to make a small summary of the very basics, for those who need a quick dive-in to the subject.
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
427
votes
12 answers

Callback functions in C++

In C++, when and how do you use a callback function? EDIT: I would like to see a simple example to write a callback function.
cpx
  • 17,009
  • 20
  • 87
  • 142
331
votes
10 answers

Passing capturing lambda as function pointer

Is it possible to pass a lambda function as a function pointer? If so, I must be doing something incorrectly because I am getting a compile error. Consider the following example using DecisionFn = bool(*)(); class Decide { public: …
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
307
votes
21 answers

What's the nearest substitute for a function pointer in Java?

I have a method that's about ten lines of code. I want to create more methods that do exactly the same thing, except for a small calculation that's going to change one line of code. This is a perfect application for passing in a function pointer…
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
288
votes
10 answers

"unpacking" a tuple to call a matching function pointer

I'm trying to store in a std::tuple a varying number of values, which will later be used as arguments for a call to a function pointer which matches the stored types. I've created a simplified example showing the problem I'm struggling to…
Flexo
  • 87,323
  • 22
  • 191
  • 272
278
votes
8 answers

Understanding typedefs for function pointers in C

I have always been a bit stumped when I read other peoples' code which had typedefs for pointers to functions with arguments. I recall that it took me a while to get around to such a definition while trying to understand a numerical algorithm…
user192712
248
votes
5 answers

Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?

Why do the following work? void foo() { cout << "Foo to you too!\n"; }; int main() { void (*p1_foo)() = foo; void (*p2_foo)() = *foo; void (*p3_foo)() = &foo; void (*p4_foo)() = *&foo; void (*p5_foo)() = &*foo; void…
Jimmy
  • 4,419
  • 6
  • 21
  • 30
246
votes
6 answers

Using generic std::function objects with member functions in one class

For one class I want to store some function pointers to member functions of the same class in one map storing std::function objects. But I fail right at the beginning with this code: #include class Foo { public: void…
Christian Ivicevic
  • 10,071
  • 7
  • 39
  • 74
208
votes
12 answers

How can I use an array of function pointers?

How should I use array of function pointers in C? How can I initialize them?
john
206
votes
18 answers

Callback functions in Java

Is there a way to pass a call back function in a Java method? The behavior I'm trying to mimic is a .Net Delegate being passed to a function. I've seen people suggesting creating a separate object but that seems overkill, however I am aware that…
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
202
votes
13 answers

C isn't that hard: void ( *( *f[] ) () ) ()

I just saw a picture today and think I'd appreciate explanations. So here is the picture: Transcription: "C isn't that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions…
Motun
  • 2,149
  • 3
  • 16
  • 23
185
votes
9 answers

How can I pass a member function where a free function is expected?

The question is the following: consider this piece of code: #include class aClass { public: void aTest(int a, int b) { printf("%d + %d = %d", a, b, a + b); } }; void function1(void (*function)(int, int)) { …
Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121
164
votes
12 answers

Function Pointers in Java

This may be something common and trivial, but I seem to be having trouble finding a concrete answer. In C# there is a concept of delegates, which relates strongly to the idea of function pointers from C++. Is there a similar functionality in Java?…
dreadwail
  • 15,098
  • 21
  • 65
  • 96
153
votes
11 answers

Calling C++ member functions via a function pointer

How do I obtain a function pointer for a class member function, and later call that member function with a specific object? I’d like to write: class Dog : Animal { Dog (); void bark (); } … Dog* pDog = new Dog (); BarkFunction pBark =…
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
151
votes
3 answers

How does the C code that prints from 1 to 1000 without loops or conditional statements work?

I've found C code that prints from 1 to 1000 without loops or conditionals : But I don't understand how it works. Can anyone go through the code and explain each line? #include #include void main(int j) { printf("%d\n", j); …
ob_dev
  • 2,808
  • 1
  • 20
  • 26
1
2 3
99 100