Function pointer

Thread Starter

aspirea5745

Joined Apr 17, 2019
99
A function pointer is a variable that stores the address of a function that can later be called through that function pointer. We can also call the function by its name or with a pointer.

When should we call a function by a pointer ?

C:
#include<stdio.h>

void function ()
{
    printf ("Task1 \n");
}

int main ()
{
    function();// calling function by its name
    
     void (*pointer)(void) = &function;
     (*pointer)();
    
     return 0;
}
 

Papabravo

Joined Feb 24, 2006
21,159
The simplistic answer is: whenever you want or need to. A trivial example might be parsing an alphanumeric string. For 26 letters and 10 digits there would be 36 functions, 36 function pointers. There would be a case statement with 36 branches and a default. Each case of an alphanumeric character would call a different function.
 

WBahn

Joined Mar 31, 2012
29,976
A function pointer is a variable that stores the address of a function that can later be called through that function pointer. We can also call the function by its name or with a pointer.

When should we call a function by a pointer ?

C:
#include<stdio.h>

void function ()
{
    printf ("Task1 \n");
}

int main ()
{
    function();// calling function by its name
   
     void (*pointer)(void) = &function;
     (*pointer)();
   
     return 0;
}
When you want to dynamically change which function is being called from the same piece of code.

Imagine you store a bunch of function pointers in an array. Now, based on the value of an index variable, you can control which function gets called.
 

MrSoftware

Joined Oct 29, 2013
2,188
One simple case, and probably most common, is for callbacks. A class might call-back to some other function when some event occurs, and the pointer to that call back function would be stored in a variable. This way the callback function can be set at run time.

A more complex case; I worked on a driver that had to call multiple functions in a row, but the functions that were called would vary depending on a few state variables that would change relatively infrequently. if() and else() are too expensive for the speed that was required, so we kept an array of function pointers and just ran down the array calling functions. When the state would change such that different functions needed to be called, we just rebuilt the array with new pointers then resumed. This was significantly faster than if/else/if/else...
 

BobaMosfet

Joined Jul 1, 2009
2,110
A function pointer is a variable that stores the address of a function that can later be called through that function pointer. We can also call the function by its name or with a pointer.

When should we call a function by a pointer ?

C:
#include<stdio.h>

void function ()
{
    printf ("Task1 \n");
}

int main ()
{
    function();// calling function by its name
   
     void (*pointer)(void) = &function;
     (*pointer)();
   
     return 0;
}
Any time you don't know the name of a function you're calling, but you know its format- you use a function pointer. Linked lists, queues, io parameter blocks, all use this technique to be able to call functions when they don't know the function's actual name.
 

ApacheKid

Joined Jan 12, 2015
1,533
One use case is finite state machines. This uses a 2D array of function pointers indexed by some integer event ID and an integer state value. The return value from the functions is the new state. This code is very elegant and easy to debug even for rather complex FSMs like lexical analyzers.
 
Top