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
137
votes
14 answers

Why are function pointers and data pointers incompatible in C/C++?

I have read that converting a function pointer to a data pointer and vice versa works on most platforms but is not guaranteed to work. Why is this the case? Shouldn't both be simply addresses into main memory and therefore be compatible?
gexicide
  • 38,535
  • 21
  • 92
  • 152
129
votes
8 answers

Function pointer to member function

I'd like to set up a function pointer as a member of a class that is a pointer to another function in the same class. The reasons why I'm doing this are complicated. In this example, I would like the output to be "1" class A { public: int f(); int…
Mike
  • 58,961
  • 76
  • 175
  • 221
127
votes
3 answers

What is the purpose of std::function and how to use it?

It is necessary for me to use std::function but I don't know what the following syntax means. std::function f_name = []() { FNAME(); }; What is the goal of using std::function? Is it to make a pointer to a function?
user2982229
  • 1,395
  • 2
  • 11
  • 13
125
votes
9 answers

C++ lambda with captures as a function pointer

I was playing with C++ lambdas and their implicit conversion to function pointers. My starting example was using them as callback for the ftw function. This works as expected. #include #include using namespace std; int main() { …
duncan
  • 6,113
  • 3
  • 29
  • 24
116
votes
8 answers

Casting a function pointer to another type

Let's say I have a function that accepts a void (*)(void*) function pointer for use as a callback: void do_stuff(void (*callback_fp)(void*), void* callback_arg); Now, if I have a function like this: void my_callback_function(struct my_struct*…
Mike Weller
  • 45,401
  • 15
  • 131
  • 151
101
votes
14 answers

How can I pass a class member function as a callback?

I'm using an API that requires me to pass a function pointer as a callback. I'm trying to use this API from my class but I'm getting compilation errors. Here is what I did from my…
ofer
  • 4,366
  • 9
  • 38
  • 39
88
votes
4 answers

Meaning of int (*) (int *) = 5 (or any integer value)

I cannot figure this out: int main() { int (*) (int *) = 5; return 0; } The above assignment compiles with g++ c++11. I know that int (*) (int *) is a pointer to a function that accepts an (int *) as argument and returns an int, but I do…
Konrad
  • 2,207
  • 4
  • 20
  • 31
85
votes
8 answers

Python function pointer

I have a function name stored in a variable like this: myvar = 'mypackage.mymodule.myfunction' and I now want to call myfunction like this myvar(parameter1, parameter2) What's the easiest way to achieve this?
schneck
  • 10,556
  • 11
  • 49
  • 74
83
votes
5 answers

How does dereferencing of a function pointer happen?

Why and how does dereferencing a function pointer just "do nothing"? This is what I am talking about: #include void hello() { printf("hello"); } int main(void) { (*****hello)(); } From a comment over here: function pointers…
Lazer
  • 90,700
  • 113
  • 281
  • 364
76
votes
2 answers

How to call through a member function pointer?

I'm trying to do some testing with member function pointer. What is wrong with this code? The bigCat.*pcat(); statement doesn't compile. class cat { public: void walk() { printf("cat is walking \n"); } }; int main(){ cat bigCat; …
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
75
votes
14 answers

How to make a function return a pointer to a function? (C++)

I'm trying to make a function that takes a character, then returns a pointer to a function depending on what the character was. I just am not sure how to make a function return a pointer to a function.
user98188
73
votes
8 answers

Returning function pointer type

Often I find the need to write functions which return function pointers. Whenever I do, the basic format I use is: typedef int (*function_type)(int,int); function_type getFunc() { function_type test; test /* = ...*/; return…
user545199
72
votes
8 answers

Does Function pointer make the program slow?

I read about function pointers in C. And everyone said that will make my program run slow. Is it true? I made a program to check it. And I got the same results on both cases. (measure the time.) So, is it bad to use function pointer? Thanks in…
drigoSkalWalker
  • 2,742
  • 9
  • 32
  • 45
70
votes
8 answers

Complex C declaration

I was just going through some code on the Internet and found this: float * (*(*foo())[SIZE][SIZE])() How do I read this declaration? Is there a specific set of rules for reading such complex declarations?
Kaunteya
  • 3,107
  • 1
  • 35
  • 66
67
votes
1 answer

How to assign a null value to a function type variable in Kotlin?

I have a variable that holds a callback, and by default it's value should be null. But this syntax doesn't seem to work. var callback1 : () -> Unit = null var callback2 : ((a) -> c, b) -> Unit = null My current solution is to make sure that…
x2bool
  • 2,766
  • 5
  • 26
  • 33