By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Array of function pointers

P: 22
Small doubt regarding function pointers as you can see the array of function pointers is initialized with two elements, but i am calling up to 3. In such cases what is the behavior of the program? How to avoid using MAX?

Expand|Select|Wrap|Line Numbers
  1. void Test1(void);
  2. void Test2(void);
  3. #define MAX 3
  4. for(i = 0; i < MAX; i++)
  5. {
  6.  (*funcptr[i])();
  7. }
  8. void (*funcptr[])(void) =
  9. {
  10.     Test1,
  11.     Test2,
  12. };
  13.  
Nov 12 '15 #1
Share this Question
Share on Google+
8 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
First, define your array of function pointers. Before you do this you have to know the arguments and return type of the functions whose addresses go in the array elements.

Second, assign the address of the functions to the array elements. If you have several functions whose addresses you are storing in the array, then MAX would not be needed because you can't use a loop to assign different functions to different elements.

Show me what your array looks like.
Nov 12 '15 #2

P: 22
This is the program i am trying to execute
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define MAX        3
  4. void Test1(void);
  5. void Test2(void);
  6.  
  7. void (*funp[])(void)=
  8.     {
  9.         Test1,
  10.         Test2,
  11.     };
  12.  
  13. int main(void)
  14. {
  15. int index=0;
  16. for(index=0;index <MAX; index++)
  17. {
  18.    (*funp[index])();    
  19. }
  20.  
  21. return 0;
  22. }
  23.  
  24. void Test1(void)
  25. {
  26.     printf("In test1\n");
  27. }
  28.  
  29. void Test2(void)
  30. {
  31.     printf("In Test2\n");
  32. }
  33.  
How to avoid using MAX because every time I add new function I have to modify MAX?
Nov 13 '15 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
You have a loop in your main() that is using an array but there is no array in the program.

What does your array look like? You can't use the index operator ( [ ] ) unless you have an array.

Like this:

Expand|Select|Wrap|Line Numbers
  1. void(*arr[3])(void);
This code says that arr is an array of three things which are pointers to functions with void arguments that return void.

What is your array supposed to contain?
Nov 13 '15 #4

P: 22
i thought this an array of function pointers.
Expand|Select|Wrap|Line Numbers
  1. void (*funp[])(void)=
  2. {
  3.   Test1,
  4.   Test2,
  5. };
  6.  
I get proper output if I use MAX equal to 2. I am getting confused.

Or should i have said it is an array of pointers to function.
Nov 13 '15 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
Sorry. you are correct. I missed that since I don't usually initialize arrays that way.

OK so in your loop you crash because MAX is 3 but there are only 2 elements in the array. Just change MAX to 2 and the code will run. Or create an array of 3 elements.
Nov 13 '15 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
BTW there is a weirdness here in C and C++.

The name of a function is the address of the function. So the address of

Expand|Select|Wrap|Line Numbers
  1. int myFunc(int);
is myFunc. You call the function by

Expand|Select|Wrap|Line Numbers
  1. myFunc(5);
There are some people who see myFunc as an address of a function so you would need to *myFunc to access the actual function:

Expand|Select|Wrap|Line Numbers
  1. *myFunc(5);
Because of this both ways of calling a function are supported.

In your case, the loop could be:

Expand|Select|Wrap|Line Numbers
  1. for (index = 0; index <MAX; index++)
  2.     {
  3.         funp[index]();
  4.     }
which is easier to read without the de-reference operator.
Nov 13 '15 #7

P: 22
I plan to modify the code to avoid using MAX. Can I use something like this
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void Test1(void);
  3. void Test2(void);
  4.  
  5. void (*funp[])(void)=
  6.     {
  7.         Test1,
  8.         Test2,
  9.     };
  10.  
  11. int main(void)
  12. {
  13. int index=0;
  14. int size = sizeof(funp)/sizeof(funp[0]);
  15. for(index=0;index < size; index++)
  16. {
  17.    (*funp[index])();    
  18. }
  19. return 0;
  20. }
  21.  
  22. void Test1(void)
  23. {
  24.     printf("In test1\n");
  25. }
  26.  
  27. void Test2(void)
  28. {
  29.     printf("In Test2\n");
  30. }
  31.  
Nov 13 '15 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
You can up to a point.

The sizeof operator gives you the size of the variable on the stack.

In this case you will get the size of two function pointers divided by the size of the function pointer in element 0. The result will be 2 and there are 2 elements in the array.

However, if the array is on the heap:

Expand|Select|Wrap|Line Numbers
  1. void(**funp)(void);
  2.  
  3.     int index = 0;
  4.  
  5.     funp =  malloc(sizeof(void(*)(void)) * 10);
  6.  
  7.     for (index = 0; index <10; index++)
  8.     {
  9.         //funp[index]();
  10.         funp[index]();
  11.  
  12.     }
  13.  
all you have is the sizeof a pointer to a function pointer. The array size of 10 elements has been lost.

So this:

Expand|Select|Wrap|Line Numbers
  1. int size = sizeof(funp)/sizeof(funp[0]);
  2.  
will always be 1.

Because of this, the common practice is to always have two variables. One for the array and one for the number of elements in the array.
Nov 13 '15 #9

Post your reply

Sign in to post your reply or Sign up for a free account.