0

Can anybody help me with this simple code??

#include <iostream>
using namespace std;

void testFunction(){
    cout<<"This is the test function 0"<<endl;
}

void testFunction1(){
    cout<<"This is the test function 1"<<endl;
}

void testFunction2(){
    cout<<"This is the test function 2"<<endl;
}

void (*fp[])()={testFunction,testFunction1,testFunction2};

int main(){

    //fp=testFunction;
    (*fp[testFunction1])();
    //cout<<"Addrees of the function pointer is:"<<*fp;
}

I am getting the following error:

error: invalid types `void (*[3])()[void ()()]' for array subscript|
1
  • 1
    That is really evil stuff, by the way. Commented Mar 31, 2010 at 8:46

3 Answers 3

7

You're trying to use a function pointer as an array index. That won't fly, array indices must be integer.

To call through a function pointer, just call:

(*fp[1])();

or the (even shorter!)

fp[1]();

will work.

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

1 Comment

Thanks ...i don't know how i missed that.
2

I think you meant to write:

(*fp[1])();

That is, you index the array with an int rather than the function itself.

Comments

2

Your indexing the array fp of functions with a function pointer, try something like:

(*fp[some_index])();

instead

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.