Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of function pointers? [duplicate]

Tags:

c

Possible Duplicate:
What is the point of function pointers?

I am trying to understand where in the practical scenarios function pointers are used. and also can anyone please give me a practical example where we have to pass function itself as an argument to another function.

like image 774
haris Avatar asked Jan 23 '12 16:01

haris


People also ask

What is the use of function pointers?

1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.

What is the use of function pointers in C?

Function Pointers point to code like normal pointers. In Functions Pointers, function's name can be used to get function's address. A function can also be passed as an arguments and can be returned from a function.

Should you use function pointers in C++?

C++ supports polymorphism directly, so there is often less need for explicit function-pointers in C++ - although internally polymorphism is implemented using function pointers in any case.

How do you initialize a pointer to a function in C++?

You need to initialize a pointer by assigning it a valid address. This is normally done via the address-of operator ( & ). The address-of operator ( & ) operates on a variable, and returns the address of the variable. For example, if number is an int variable, &number returns the address of the variable number .


2 Answers

Function pointers can be useful when you want to create callback mechanism, and need to pass address of a function to another function.

They can also be useful when you want to store an array of functions, to call dynamically for example.

like image 159
Tony The Lion Avatar answered Sep 29 '22 13:09

Tony The Lion


Callback routines appear to be the most common scenario put forth thus far. However, there are many others ...

Finite State Machines where the elements of (multi-dimensional) arrays indicate the routine that processes/handles the next state. This keeps the definition of the FSM in one place (the array).

Enabling features and disabling of features can be done using function pointers. You may have features that you wish to enable or disable that do similar yet distinct things. Instead of populating and cluttering your code with if-else constructs testing variables, you can code it so that it uses a function pointer, and then you can enable/disable features by changing/assigning the function pointer. If you add new variants, you don't have to track down all your if-else or switch cases (and risk missing one); instead you just update your function pointer to enable the new feature, or disable the old one.

Reducing code clutter I touched upon this in the previous example. Examples such as ...

switch (a) { case 0:     func0();     break; case 1:     func1();     break; case 2:     func2();     break; case 3:     func3();     break; default:     funcX();     break; } 

Can be simplified to ...

/* This declaration may be off a little, but I am after the essence of the idea */ void (*funcArray)(void)[] = {func0, func1, func2, func3, funcX}; ... appropriate bounds checking on 'a' ... funcArray[a](); 

There are many more. Hope this helps.

like image 35
Sparky Avatar answered Sep 29 '22 14:09

Sparky