2

I have got 2 functions:

char* odwroc(char* nap, int n) 
char* male(char* nap, int n) 

I have defined a pointer to that kind functions

 typedef char*(*pointerToFunction )( char*, int );

then in used that definition in main:

 pointerToFunction ptr1 = odwroc;
 pointerToFunction ptr2 = male;

but now I have to create a function which as a first parameter gets array of that pointers to function and I am stuck. I don't know how to define array of pointers to function and how the modyfikuj parameter list should look like.

void modyfikuj(char* pointerToFunction *pointerArray, int length, char* nap2, int n){ 
}
3
  • How would you declare an argument as an array of any other type? As pointerToFunction is a type (alias of a type really) it can be used as any other type like char or int. Commented Apr 14, 2014 at 9:05
  • @JoachimPileborg Like that: void modyfikuj(pointerToFunction *pointerArray, int length, char* nap2, int n){ } Commented Apr 14, 2014 at 9:07
  • That looks good. Now make an array of the type, and pass it to the function. Commented Apr 14, 2014 at 9:07

2 Answers 2

1

Try this:

pointerToFunction mojefunkcje[] = { odwroc, male};

modyfikuj( mojefunkcje, ...);      // pass the array fo modyfikuj()

void modyfikuj( pointerToFunction* funtab, ...)
{
    funtab[0]( string, liczba);    // call  odwroc( string, liczba)
    funtab[1]( string, liczba);    // call  male( string, liczba)
}
Sign up to request clarification or add additional context in comments.

Comments

1

Even though the above answer make sense, use of containers such as std::vector will give you more control when passing an array of similar type such as a pointer to a function. Please try below code snippet.

#include "vector"
using namespace std;

typedef char*(*pointerToFunction )( char*, int );

typedef vector<pointerToFunction> FUNCTION_VECTOR;

bool modyfikuj( FUNCTION_VECTOR& vecFunctionVector )
{
    // The below checking ensures the vector does contain at least one function pointer to be called.
    if( vecFunctionVector.size() <= 0 )
    {
        return false;
    }

    // You can have any number of function pointers to be passed and get it executed, one by one.
    FUNCTION_VECTOR::iterator itrFunction = vecFunctionVector.begin();
    FUNCTION_VECTOR::const_iterator itrFunEnd = vecFunctionVector.end();
    char* cszResult = 0;
    for( ; itrFunEnd != itrFunction; ++itrFunction )
    {
        cszResult = 0;
        // Here goes the function call!
        cszResult = (*itrFunEnd)( "Hello", 1 );

        // Check cszResult for any result.
    }

    return true;

}

char* odwroc(char* nap, int n); // You will define this function somewhere else.
char* male(char* nap, int n); // You will define this function somewhere else.

int main()
{
    FUNCTION_VECTOR vecFunctions;
    // You can push as many function pointers as you wish.
    vecFunctions.push_back( odwroc );
    vecFunctions.push_back( male );
    modyfikuj( vecFunctions );
    return  0;
}

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.