0

I have used function pointer in my application. But recently i come across one function pointer which is called array of function pointer. I couldn't able to understand how it will work. Below is the example,

typedef uint8 (*fnHandlr)( uint16 iD, uint32 Process);

const funHandlr arr[] = 
{
    fun1, fun2, fun3
}
uint8 fun1(uint16 iD, uint32 Process)
{
     /*Piece of code*/
}
uint8 fun3(uint16 iD, uint32 Process)
{
     /*Piece of code*/
}
uint8 fun3(uint16 iD, uint32 Process)
{
     /*Piece of code*/
}

my questions are below,

  1. What is the use of typedef here.
  2. If funHandlr is called, whether all the functions in the array will be called?
  3. what is arr[] mean. How many bytes the compiler will allocate?
0

2 Answers 2

1
  1. The typedef is used to simplify the declaration of the array of function-pointers.
  2. As funHandlr is a typedef-name, it cannot be called.

    Neither can the array of implicit length 3 (arr) be called, as it is neither a function nor a function-pointer.

    A value of type funHandlr is a callable function-pointer though.

  3. const funHandlr arr[] = { ... }; defines an array named arr of deduced length (3, from the initializers) of const funHandlr, funHandlr being the type defined above.
Sign up to request clarification or add additional context in comments.

6 Comments

Then how the fun1, fun2, fun2 will execute
I don't quite get your meaning, sorry. Could you elaborate?
I mean, if i want to call fun1. How can i call. can i call directly fun1(). if yes, then what is use of array of function
@Reshmikhanna It allows arr[expr]() with 0<=expr && expr<3.
Then what is the use of array of function?. The fun1 can be call directly
|
1
  1. The typedef makes it easier for humans to "parse" the definition of the function pointer. It serves no other purpose here, because it's used only once.
  2. funHandlr cannot be "called" because it is a type. An object of type funHandlr (e.g. any of the array elements) can be called in the same way that you call a function pointer.
  3. arr is the name of the array of function pointers. When you wish to call a function, apply an index to it, and then put parentheses to make the call.

Here is an example of how to call one of the functions based on its index:

int index;
printf("Which function you wish to call? Enter 0, 1, or 2 >");
scanf("%d", &index);
// Make a call like this:
arr[index](myId, myProc);

What is the use of array of functions if we can fun1, fun2, or fun3 directly?

Consider what would you do to write a program similar to the one above without function pointers. Here is how your code would look:

scanf("%d", &index);
switch (index) {
case 0: fun1(myId, myProc); break;
case 1: fun2(myId, myProc); break;
case 2: fun3(myId, myProc); break;
}

Apart from being repetitive and error-prone, switch statements like this create a maintenance liability in cases when you wish to change the signature of your function. It is not uncommon for arrays of function pointers to grow to more than a hundred items; adding a new parameter to each invocation would require a lot of effort.

With an array of function pointers, however, there is only one call to change. Moreover, the readers of your code would not have to scroll through hundreds of lines in order to understand what is going on.

1 Comment

If this the case, then what is the use of array of functions and function pointer funHandlr?. Directly we can fun1 or fun 2, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.