3

I want to make a function pointer array and be able to call them in a for-loop. How can I achieve this? I have tried:

void (**a) (int);
a[0] = &my_func1;
a[1] = &my_func2;
a[2] = &my_func3;

for ( i = 0; i < 3; i++){
    a[0]();
    (*a[0])(); // Neither does work
}

But I am missing some syntax I guess:

error: too few arguments to function ‘*(a + (long unsigned int)((long unsigned int)i * 8ul))’
1
  • 1
    You should probably allocate some space for that array... Commented May 19, 2012 at 15:01

7 Answers 7

7

The function you declare is expected to take an int as a parameter:

a[0](1);

Also note that you declare a pointer to pointer for the functions, but you don't allocate any memory for them (I assume this is only in the example) Otherwise it should probably be:

void (*a[3]) (int);
Sign up to request clarification or add additional context in comments.

Comments

3

You are declaring that a is a pointer to a pointer to (or an array of pointers to) a function that takes an int as a parameter - so you need to pass an int when you call the functions, e.g. a[0](42);.

Comments

2

I guess the below code is what you need.

typedef void * func_pointer(int);

func_pointer fparr[10];

for(int i = 0; i<10; i++)
{
     fparr[i](arg); //pass the integer argument here
}

1 Comment

maybe a typo in the typedef statement, I encounter an error when compile the code, and typedef void (*func_pointer)(int); works. Pls double check:).
1

1) Where have you allocated or defined array to store function addresses?

2) in loop you are always calling (*a[0])();,There should be loop counter

Comments

1

You forgot to give an argument to your function.

void (**a) (int); // here it takes an int argument
a[0] = &my_func1;
a[1] = &my_func2;
a[2] = &my_func3;

for ( i = 0; i < 3; i++){
    a[0](); // here you do not give an argument
}

But be careful, you do not allocate memory to your a array, and it fails with a nice segmentation fault error.

void my_func1(int i) {
    ;
}
void my_func2(int i) {
    ;
}
void my_func3(int i) {
    ;
}

int main() {
    void (**a) (int);
    a = malloc(3*sizeof(void*)); // allocate array !
    a[0] = &my_func1;
    a[1] = &my_func2;
    a[2] = &my_func3;

    for (int i = 0; i < 3; i++){
        a[i](1); // respect your own function signature
    }
    free(a); // it's always a good habit to free the memory you take
    return 0;
}

Comments

1

You can typedef void (*pfun)(int); and then pfun a[3]; is the array you want.

The following code may work for you:

typedef void (*pfun)(int);

int main() {
    pfun a[3];
    a[0] = myfunc1;    // or &myfunc1 whatever you like
    a[1] = myfunc2;
    a[2] = myfunc3;
}

Comments

1

You can define your function-array with the needed size and initialize it with your functions like:

void my_func1(int x){}
void my_func2(int x){}
void my_func3(int x){}

void (*a[])(int)={my_func1,my_func2,my_func3};


int i;
for(i=0;i<sizeof a/sizeof*a;++i)
  a[i](i);

The address-operator '&' before any function-name is redundant.

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.