0

I am writing a program in C which uses an array of function pointers. If I do not include an argument I am able to call the functions with my code without an issue.

int (*functionsArray[2][3])() = {
  {functionOne,functionTwo,functionThree},
  {functionFour,functionFive,functionSix}
};

However when I try and pass the argument int x:

int (*functionsArray[2][3])(int x) = {
  {functionOne,functionTwo,functionThree},
  {functionFour,functionFive,functionSix}
};

I get an error:

invalid conversion from 'int (*)()' to 'int (*)(int)'

Also none of these functions return an int, shouldn't I be able to declare them as void?

void(*functionsArray[2][3])(int x) = {
  {functionOne,functionTwo,functionThree},
  {functionFour,functionFive,functionSix}
};

Trying this results in an error:

 error: invalid conversion from 'int (*)()' to 'void (*)(int)'

Thanks.

3
  • Show us at least functionOne and show us at which line you get the error. Commented Oct 6, 2014 at 6:45
  • functionOne, functionTwo, ... should have all exactly the same prototype. The prototype should be same as the prototype you use for the array. Commented Oct 6, 2014 at 6:53
  • Have you tried reorganising the parentheses, maybe something like (void(*functionsArray(int x)))[2][3]) = ... . Secondly, and far more importantly, I would STRONGLY recommend creating a typedef for the function pointer, so the usage can be straight firmware array declaration. Commented Oct 6, 2014 at 6:56

2 Answers 2

1

That will work fine, provided you declare the functions correctly:

#include <stdio.h>

int functionOne(int x)   { return 1; }
int functionTwo(int x)   { return 2; }
int functionThree(int x) { return 3; }
int functionFour(int x)  { return 4; }
int functionFive(int x)  { return 5; }
int functionSix(int x)   { return 6; }

int (*functionsArray[2][3])(int x) = {
    {functionOne,  functionTwo,  functionThree},
    {functionFour, functionFive, functionSix}
};

int main (void) {
    printf ("%d\n", (functionsArray[0][1])(99));
    printf ("%d\n", (functionsArray[1][2])(99));
    return 0;
}

The output of that program is 2 and 6.


It will also work if you want no return value:

#include <stdio.h>

void functionOne(int x)   { puts ("1"); }
void functionTwo(int x)   { puts ("2"); }
void functionThree(int x) { puts ("3"); }
void functionFour(int x)  { puts ("4"); }
void functionFive(int x)  { puts ("5"); }
void functionSix(int x)   { puts ("6"); }

void (*functionsArray[2][3])(int x) = {
    {functionOne,  functionTwo,  functionThree},
    {functionFour, functionFive, functionSix}
};

int main (void) {
    (functionsArray[0][1])(99);
    (functionsArray[1][2])(99);
    return 0;
}

That program also outputs 2 and 6, as expected.


It all comes down to ensuring that the function declarations match the type given in the array.

Sign up to request clarification or add additional context in comments.

1 Comment

gee I feel dumb. That fixed it up, thanks very much!
0

When you declare an array:

int (*functionsArray[2][3])(int x) = 
{
   {functionOne,functionTwo,functionThree},
   {functionFour,functionFive,functionSix}
};

every element of the array has to be of type int (*)(int). Otherwise, the compiler correctly reports an error.

Take a simple case:

void foo()
{
}

int (*fp)(int x) = foo;

should result in the same compiler error because you are trying to initialize a variable of type int (*)(int) using foo, whose type is void (*)().

Comments

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.