0

I am trying to understand the syntax to call the function through a pointer to an array of function pointers. I have array of function pointers FPTR arr[2], and a pointer to this array FPTR (vptr)[2] . But it gives me an error when trying to call through the pointer to an array

typedef int (*FPTR)();
int func1(){
        cout<<"func1() being called\n";
}
int func2(){
        cout<<"fun2() being called\n";
}

    FPTR arr[2] = {&func1,&func2};

    FPTR (*vptr)[2];
    vptr=&arr;

    cout<<"\n"<<vptr[0]<<endl;
    cout<<"\n"<<vptr[0]()<<endl;  // ERROR  when trying to call the first function
3
  • 1
    vptr is pointer, so first you should dereference it before accesing elements of array pointed to by it: (*vptr)[0](). Commented Sep 9, 2019 at 5:11
  • I had tried that but i couldn't understand why (*vptr)[0] prints 1 always no matter for [0] or [1] or any other element of an array. Sameways, cout<<func1 or cout<<&func1 also prints 1 always Commented Sep 9, 2019 at 5:14
  • 1
    If you want to print pointer you need to cast to void* otherwise ostream::operator<<(bool) is called. cout<<"\n"<< (void*)(*vptr)[0]<<endl; Commented Sep 9, 2019 at 5:21

3 Answers 3

5

vptr is pointer to an array, so you must dereference it to use the array.

#include <iostream>
using std::cout;
using std::endl;

typedef int (*FPTR)();
int func1(){
        cout<<"func1() being called\n";
        return 0;
}
int func2(){
        cout<<"fun2() being called\n";
        return 2;
}

int main(){
    FPTR arr[2] = {&func1,&func2};

    FPTR (*vptr)[2];
    vptr=&arr;

    cout<<"\n"<<vptr[0]<<endl;
    cout<<"\n"<<(*vptr)[0]()<<endl;
}

live example

Note that func1() and func2() must return value, or outputting their results would cause undefined behavior

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

Comments

2
typedef int (*FPTR)();
int func1(){
        cout<<"func1() being called\n";
        return 1;

}
int func2(){
        cout<<"fun2() being called\n";
        return 2;
}

FPTR arr[2] = {func1, func2}; 

// call both methods via array of pointers
cout<<"\n"<< arr[0]() <<endl;
cout<<"\n"<< arr[1]() <<endl;

FPTR (*vptr)[2] = &arr;

// call both methods via pointer to array of pointers
cout<<"\n"<< vptr[0][0]() <<endl;
cout<<"\n"<< vptr[0][1]() <<endl;

// or... 
cout<<"\n"<< (*vptr)[0]() <<endl;
cout<<"\n"<< (*vptr)[1]() <<endl;

1 Comment

Note that func1 and func2 does not return any value so cout << (*vptr)[0]() will cause undefined behavior
2

A pointer to an array is not needed here. A pointer to the first array element works.

FPTR *vptr;
vptr = arr;

// vptr[0]() works

A reference to an array is also ok.

FPTR (&vptr)[2] = arr;

// vptr[0]() still works

If for some reason you need a pointer to an array, you can:

FPTR (*vptr)[2];
vptr = arr;

// (*vptr)[0]() works

To avoid confusion, prefer std::array to plain arrays.

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.