0

After creating an array of function pointers void(*array[d1])(int);

How can I pass it as a parameter of a function?

callFunctions(int array[],int size){
    for(int i=0;i<size;i++){
        *array[i](0);
    }
}

Compiler keeps saying my parameter type is wrong

3
  • I would prefer syntax with std::array<void(*)(int), d1>. Commented Jan 30, 2017 at 11:45
  • interesting, maybe I'm just too novice to see the difference Commented Jan 31, 2017 at 1:44
  • IMO, It is clearer, in particular to pass/return argument: void foo(const std::array<void(*)(int), d1>& a) against void foo(void(*(&a)[d1])(int)). using/typedef might also be used. Commented Jan 31, 2017 at 9:44

1 Answer 1

3

Call the functions using the function pointer array like following:

 (*array[i]) ( 0 );

Update you callFunctions as:

/* void */ callFunctions( void (*fptr[ ])(int), int size )
{
   // (*fptr[ i ])( 0 );
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that, but still doesn't like how I am passing the array in the function
@R.Velazquez Sorry didn't noticed. I updated the post.
array[i] ( 0 ); is also a legal call but dereference notation helps to figure out how to write the pointer

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.