# Array of functions

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-29-2010
frktons
Array of functions
Hello everybody.

While I was looking for a way to call different functions knowing a number
related to the function, I mean n = 1 relates to function() number 1, n = 2 relates to
but for C++.

Do you know if it can be done in C as well? What would be different between C and C++?

Thanks

Code:

```Array of Functions Labels: Arrays, Functions, Miscellaneous, Pointers In the article Pointers to Function, we saw how pointers can be made to point at functions and hence can be used to invoke them. By far the most important use of pointers to functions is to have arrays of functions. This can be achieved as stated below You already know that we can have arrays of pointers and pointers can be made to point at functions. So combining both we can have array of pointers to functions put differently, we can have array of functions. The example program below demonstrates how we can have array of functions; please note that this concept is mostly used in writing compilers and interpreters, so you shouldn’t expect the program to do anything serious or useful!   // Program to demonstrate   // array of functions   #include<iostream.h>   // -- FUNCTION PROTOTYPES --   void func1();   void func2();   void func3();   void func4();   void func5();   // -- ENDS --   void main()   {   // notice the prototype   void (*ptr[5])();   // arrays are made to point   // at the respective functions   ptr[0]=func1;   ptr[1]=func2;   ptr[2]=func3;   ptr[3]=func4;   ptr[4]=func5;   // now the array elements   // point to different functions   // which are called just like   // we access the elements of   // an array   for(int i=0;i<5;i++)     (*ptr[i])();   }   // -- FUNCTIONS DEFINITION --   void func1()   {   cout<<"Called Func1!\n";   }   void func2()   {   cout<<"Called Func2!\n";   }   void func3()   {   cout<<"Called Func3!\n";   }   void func4()   {   cout<<"Called Func4!\n";   }   void func5()   {   cout<<"Called Func5!\n";   }   // -- ENDS --```
• 06-29-2010
quzah
Replace all of the cout lines, and try it. Yes, there are function pointers in C.

Quzah.
• 06-29-2010
frktons
Quote:

Originally Posted by quzah
Replace all of the cout lines, and try it. Yes, there are function pointers in C.

Quzah.

Code:

`#include<iostream.h>`
I have no header with this name in my C compiler.
Maybe
Code:

`#include <stdio.h>`
is the substitute?

Thanks
• 06-29-2010
Yes, of course! :)

You can't be afraid to just try these idea's - consider that it took you many times longer to post your question, than it would have taken you to just try it. :(

And that doesn't take into account your time lost while you wait around for an answer.

Learning programming implies, indeed requires, some experimentation on your part.
• 06-29-2010
frktons
Quote:

Yes, of course! :)

You can't be afraid to just try these idea's - consider that it took you many times longer to post your question, than it would have taken you to just try it. :(

And that doesn't take into account your time lost while you wait around for an answer.

Learning programming implies, indeed requires, some experimentation on your part.

Well, I'm not afraid to try my ideas, I just enjoy more to interact with people. :-)

Time doesn't matter when you enjoy. I could search a million web sites and find all the
answers I need, and the code, and everything else. But it would be less fun :-(

To be honest I'm not learning programming, I'm just learning C. It is quite different.

Enjoy
• 06-29-2010
Scramble
Code:

```#include <stdio.h> void func1() {        printf("Function 1 Called\n"); } void func2() {        printf("Function 2 Called\n"); } void func3() {        printf("Function 3 Called\n"); } int main(int argc, char *argv[]) {         static void (*ptr[3])() = {func1,func2,func3};         int k=0;         for(k=0;k<3;k++)                 ptr[k]();         return 0; }```
illustrates the C equivalent of how to arrange an array of function pointers.
• 06-30-2010
Elysia
Note that the example uses void main, but you should not.
Also might be my preference, but I always put & before functions when taking their addresses. Not required, but since since you have to do it for variables, why not functions?
• 06-30-2010
frktons
Quote:

Originally Posted by Scramble
Code:

```#include <stdio.h> void func1() {        printf("Function 1 Called\n"); } void func2() {        printf("Function 2 Called\n"); } void func3() {        printf("Function 3 Called\n"); } int main(int argc, char *argv[]) {         static void (*ptr[3])() = {func1,func2,func3};         int k=0;         for(k=0;k<3;k++)                 ptr[k]();         return 0; }```
illustrates the C equivalent of how to arrange an array of function pointers.

Thanks, very useful.

Quote:

Originally Posted by Elysia
Note that the example uses void main, but you should not.
Also might be my preference, but I always put & before functions when taking their addresses. Not required, but since since you have to do it for variables, why not functions?

You are right, indeed. I just copied the article, I'm not going to use void main() . :-)

Something interesting is one sentence of the article I posted:
Code:

`please note that this concept is mostly used in writing compilers and interpreters`
similar to an interpreter. ;-)

Using your suggestions, I modified the routine, and now it works in C:
Code:

```  // Program to demonstrate   // array of functions   #include<stdio.h>   // -- FUNCTION PROTOTYPES --   void func1(void);   void func2(void);   void func3(void);   void func4(void);   void func5(void);   // -- ENDS --   int main(void)   {   // notice the prototype   void (*ptr[5])();   // arrays are made to point   // at the respective functions   ptr[0]=&func1;   ptr[1]=&func2;   ptr[2]=&func3;   ptr[3]=&func4;   ptr[4]=&func5;   // now the array elements   // point to different functions   // which are called just like   // we access the elements of   // an array   for(int i=0;i<5;i++)     ptr[i]();   return 0;   }   // -- FUNCTIONS DEFINITION --   void func1(void)   {   printf("Called Func1!\n");   }   void func2(void)   {   printf("Called Func2!\n");   }   void func3(void)   {   printf("Called Func3!\n");   }   void func4(void)   {   printf("Called Func4!\n");   }   void func5(void)   {   printf("Called Func5!\n");   }```
and the output:
Code:

```Called Func1! Called Func2! Called Func3! Called Func4! Called Func5! Press any key to continue...```
Thanks everybody
• 06-30-2010
Elysia
You may also want to know of a trick to simplify the code.
Function pointer declarations are horrible, and even more so if you're going to return them from a function. Thankfully, we can abstract this by creating a type alias:

typedef void (FncPtr_t)();
FncPtr_t* ptr[5];

Looks much better, don't you think?
• 06-30-2010
frktons
Quote:

Originally Posted by Elysia
You may also want to know of a trick to simplify the code.
Function pointer declarations are horrible, and even more so if you're going to return them from a function. Thankfully, we can abstract this by creating a type alias:

typedef void (FncPtr_t)();
FncPtr_t* ptr[5];

Looks much better, don't you think?

Good, I'll consider this option as well, thanks.

Now let's move one step further. What if I'd like to declare an array of functions
each taking 2 char parameters? Can anyone show me some implementation of this?

Would it be something like:

Code:

```typedef void (FncPtr_t)(char, char); FncPtr_t* ptr[5];```
or what?

Thanks
• 06-30-2010
Elysia
Yes, that's correct.
• 06-30-2010
frktons
Array of functions
OK.

Now I've tried to implement the parameters, based on the previous version.
Next I'll try the version proposed by Elysia.
Code:

```  // Program to demonstrate   // array of functions   #include<stdio.h>   // -- FUNCTION PROTOTYPES --   void func1(char, char);   void func2(char, char);   void func3(char, char);   void func4(char, char);   void func5(char, char);   // -- ENDS --   int main(void)   {   // notice the prototype   void (*ptr[5])(char, char);   // arrays are made to point   // at the respective functions   ptr[0]=&func1;   ptr[1]=&func2;   ptr[2]=&func3;   ptr[3]=&func4;   ptr[4]=&func5;   // now the array elements   // point to different functions   // which are called just like   // we access the elements of   // an array   for(int i=0;i<5;i++)     ptr[i]('A',' ');   return 0;   }   // -- FUNCTIONS DEFINITION --   void func1(char token, char times)   {   printf("Called Func1!\n");   printf("Token = \'%c\' - to repeat %d times\n",token,times);   }   void func2(char token, char times)   {   printf("Called Func2!\n");   printf("Token = \'%c\' - to repeat %d times incremented by 1\n",token,times);   }   void func3(char token, char times)   {   printf("Called Func3!\n");   printf("Token = \'%c\' - to repeat %d times doubled\n",token,times);   }   void func4(char token, char times )   {   printf("Called Func4!\n");   printf("Token = \'%c\' - to repeat %d times doubled except the last time\n",token,times);   }   void func5(char token, char times)   {   printf("Called Func5!\n");   printf("Token = \'%c\' - to repeat %d times tripled\n",token,times);   }```
and the output:
Code:

```Called Func1! Token = 'A' - to repeat 32 times Called Func2! Token = 'A' - to repeat 32 times incremented by 1 Called Func3! Token = 'A' - to repeat 32 times doubled Called Func4! Token = 'A' - to repeat 32 times doubled except the last time Called Func5! Token = 'A' - to repeat 32 times tripled Press any key to continue...```
• 06-30-2010
Elysia
You are printing a char with %d. This is obviously a bad idea™.
Either you pass an int or you cast the char to an int before passing it to printf.
• 06-30-2010
frktons
Quote:

Originally Posted by Elysia
You are printing a char with %d. This is obviously a bad idea™.
Either you pass an int or you cast the char to an int before passing it to printf.

According to what I've read, chars have a double nature, and can
be used in both ways. What are your comments related to? I
mean what risk you are pointing to?
• 06-30-2010
Elysia
Yes, they can be used in many ways. It is a storage of 1 byte, but it's usually used to represent characters.
But that's not the problem. You can safely pass <= 255 to it, but you are lying to printf by saying you are passing an int:
printf("Token = \'%c\' - to repeat %d times tripled\n",token,times);
Either you pass an int from the beginning:
Code:

```  void func5(char token, int times) {     printf("Called Func5!\n");     printf("Token = \'%c\' - to repeat %d times tripled\n",token,times); }```
Or you cast the char to an int:
Code:

```  void func5(char token, char times) {     printf("Called Func5!\n");     printf("Token = \'%c\' - to repeat %d times tripled\n",token,(int)times); }```
The format specifier is there to tell printf what types you are passing to it, so you can obviously not lie. That will cause undefined behavior.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last