473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Array of function pointers

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
8 2153
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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

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

Similar topics

8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
13
by: munni | last post by:
hi i want to write a program on function pointers where i have not written any programs using function pointers till now. i want to take an array of 26 functions which r to be pointed by this...
3
by: Bilgehan.Balban | last post by:
Hi, How do I declare an array of function pointers and assign each element of the array with public member functions of a class? Is it possible that the array is not a member of the class? ...
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
6
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
3
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite...
1
by: Puddle | last post by:
Hi everyone, I'd just like to preface by saying any input or direction in this would be very much appreciated; i've been working on this endlessly and can't figure out what's going wrong. I'm...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.