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
34
votes
5 answers

Why the size of a pointer to a function is different from the size of a pointer to a member function?

Isn't a pointer just an address? Or I'm missing something? I tested with several types of pointers: pointers to any variables is the same (8B on my platform) pointers to functions are the same size, as pointers to variables (8B again) pointers to…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
33
votes
1 answer

Function Pointer - Automatic Dereferencing

Possible Duplicate: How does dereferencing of a function pointer happen? void myprint(char* x) { printf("%s\n", x); } int main() { char* s = "hello"; void (*test)(char*); void (*test2)(char*); test = myprint; …
Sean Nilan
  • 1,745
  • 1
  • 14
  • 21
33
votes
1 answer

Cannot move out of captured outer variable in an `Fn` closure

I'm trying to figure out how to send a function through a channel, and how to avoid extra cloning in order to execute the function at the other end. If I remove the extra cloning operation inside the closure, I get the following error: error:…
nathansizemore
  • 3,028
  • 7
  • 39
  • 63
33
votes
2 answers

Using a function pointer with a trailing return type

There are some Stack Overflow users who strongly advocate always using the new C++11 trailing return type convention when writing functions, such as main()->int. I can see advantages, as it makes the notation uniform. However, when declaring a…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
33
votes
8 answers

Virtual Methods or Function Pointers

When implementing polymorphic behavior in C++ one can either use a pure virtual method or one can use function pointers (or functors). For example an asynchronous callback can be implemented by: Approach 1 class Callback { public: Callback(); …
doron
  • 27,972
  • 12
  • 65
  • 103
33
votes
4 answers

Function pointer vs Function reference

In the code below, function-pointer and what i considered as "function-reference" seems to have identical semantics: #include using std::cout; void func(int a) { cout << "Hello" << a << '\n'; } void func2(int a) { cout << "Hi" <<…
John
  • 1,856
  • 2
  • 22
  • 33
33
votes
6 answers

What is meaning of a pointer to a constant function?

Pointers can be declared as pointing to mutable (non-const) data or pointer to constant data. Pointers can be defined to point to a function. My coworkers and I were discussing the use of "const" with pointers and the question came up regarding the…
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
32
votes
5 answers

how to use std::function to point to a function template

#include int func(int x, int y) { return x + y; } int main() { typedef std::function Funcp; Funcp funcp = func; return 0; } But is it possible to point to a template function? #include…
hidayat
  • 9,493
  • 13
  • 51
  • 66
32
votes
3 answers

Difference of type between "fun" and "&fun"?

Do expressions fun and &fun have the same type or not? Consider the following code: template void check(T) { static_assert(is_same::value); } void…
NPS
  • 6,003
  • 11
  • 53
  • 90
32
votes
3 answers

Difference between capturing and passing an argument in lambda functions

I understand the lambda function and the purpose of it in c++ 11. But i do not understand the difference between "Capturing the value" and "Passing an argument". For Instance.. #include #include using namespace std; int…
01000001
  • 833
  • 3
  • 8
  • 21
32
votes
9 answers

Executing machine code in memory

I'm trying to figure out how to execute machine code stored in memory. I have the following code: #include #include int main(int argc, char* argv[]) { FILE* f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); unsigned…
user47322
31
votes
4 answers

Why can function pointers be `constexpr`?

How does the compiler know where in memory the square root will be before the program is executed? I thought the address would be different everytime the program is executed, but this works: constexpr double(*fp)(double) = &sqrt; cout <<…
Coolwater
  • 703
  • 8
  • 15
30
votes
3 answers

In Objective C what is the equivalent of passing a function pointer in C?

@implementation ThisObject -(void)start { SomeOtherObject *someOtherObject = [SomeOtherObject alloc]; [someOtherObject doSomethingAndCallThisFunctionWhenUrDone:myCallBackFunction :self]; } -(void)myCallBackFunction { //…
Joe
  • 301
  • 1
  • 4
  • 5
30
votes
4 answers

Is void *function() a pointer to function or a function returning a void*?

I'm confused about the meaning of void *function(). Is it a pointer to function or a function returning void*? I've always used it on data structures as a recursive function returning a pointer, but when i saw a code in multithreading (pthread)…
user9515151
  • 313
  • 1
  • 3
  • 6
30
votes
5 answers

C function pointer casting to void pointer

I am trying to run the following program but getting some strange errors: File 1.c: typedef unsigned long (*FN_GET_VAL)(void); FN_GET_VAL gfnPtr; void setCallback(const void *fnPointer) { gfnPtr = *((FN_GET_VAL*) (&fnPointer)); } File…
Manoj
  • 301
  • 1
  • 3
  • 3