1

Say we have 2 functions

foo() { cout << "Hello"; }
foo2() { cout << " wolrd!"; }

how can i create an array of pointers (say a, b), with a pointing to foo() and b to foo2() ? my goal is to store these pointers in an array A, then loop over A to execute these functions.

6
  • 1
    Since you're using C++ and not C use std::function, not raw pointers. Commented Oct 21, 2021 at 7:25
  • 1
    @Jepessen You don't need std::function as long as required. Normal function pointer type will also work for the given case. Commented Oct 21, 2021 at 7:26
  • 1
    @Jepessen: it is raw OWNING pointer we should avoid. regular pointer might be fine. Commented Oct 21, 2021 at 7:29
  • You forgot return type of function, whatever problems you have it's probably because compiler is in GNU (or other non-compliant) mode and default type is int. Commented Oct 21, 2021 at 8:06
  • @Jarod42 my goal is to store these pointers in an array A, then loop over A to execute these functions. In my opinion std::function is a better, cleaner and safer solution for achieving his goal. Then it's obviously possible to use raw pointers. Commented Oct 22, 2021 at 9:04

4 Answers 4

1

You can use typed function pointers as follows:

using FunPtrType = void(*)();

FunPtrType arr[]{&foo,  &foo2};
// or
std::array<FunPtrType, 2> arr2{&foo,  &foo2};

// ... do something with the array of free function pointers
// example
for(auto fun: arr2)
    fun();
Sign up to request clarification or add additional context in comments.

8 Comments

I would recommend against type aliases for pointers. They make the program less obvious since they hide the asterisk.
im getting a "incomplete type is not allowed" on line std::array<FunPtrType, 2> arr2{&foo, &foo2}; any ideas?
did you #include <array>? You do not need to specify the template parameters since C++17: godbolt.org/z/jMME54r75 Ah, and you forgot to specify the return type of your functions foo() ==> void foo()
@eerorika (IMO) the using makes the function pointer types better readable than some retunr type(*)( a lot of arguments) + easily editable throughout the code-base.
@eerorika matter of style. Some may argue that void (*arr[])() {&foo, &foo2}; is less readable. It looks more like some lambda suffering a transport incident
|
0

There is a simple implementation:

#include <iostream>
#include <vector>
using namespace std;
// Defining test functions
void a(){cout<<"Function A"<<endl;}
void b(){cout<<"Function B"<<endl;}

int main()
{
    /*Declaring a vector of functions 
      Which return void and takes no arguments.
    */
    vector<void(*)()> fonc;
    //Adding my functions in my vector
    fonc.push_back(a);
    fonc.push_back(b);
    //Calling with a loop.
    for(int i=0; i<2; i++){
        fonc[i]();
    }
    return 0;
}


Comments

0

There's no need for typedefs these days, just use auto.

#include <iostream>
void foo1() { std::cout << "Hello"; }
void foo2() { std::cout << " world!"; }
auto foos = { &foo1, &foo2 };
int main() { for (auto foo : foos) foo(); }

Comments

0

There are two equivalent ways to do what you want:

Method 1


#include <iostream>
void foo() 
{ 
    std::cout << "Hello";
}
void foo2() 
{ 
    std::cout << " wolrd!"; 
    
}


int main()
{
   
    void (*a)() = foo;// a is a pointer to a function that takes no parameter and also does not return anything
    
    void (*b)() = foo2;// b is a pointer to a function that takes no parameter and also does not return anything
    
    
    //create array(of size 2) that can hold pointers to functions that does not return anything and also does not take any parameter
    void (*arr[2])() = { a, b};
    
    arr[0](); // calls foo 
    
    arr[1](); //calls foo1
    
    return 0;
}

Method 1 can be executed here.

Method 2


#include <iostream>
void foo() 
{ 
    std::cout << "Hello";
}
void foo2() 
{ 
    std::cout << " wolrd!"; 
    
}


int main()
{
   
    //create array(of size 2) that can hold pointers to functions that does not return anything
    void (*arr[2])() = { foo, foo2};
    
    arr[0](); // calls foo 
    
    arr[1](); //calls foo1
    
    return 0;
}

Method 2 can be executed here.

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.