i can’t understand this variable type in a c vector declaration duplicate

This vector is declared to hold function pointers, that is, pointers to functions. As declared, it allows you to add pointers to functions that look like this:

void* someFunc(void* someArg);

Then you can use the function pointers stored in the vector to… call them, as

Here is a simple example. I don’t do anything with the void* arg but in real code, I would assume it will be used for something:

#include <iostream>
#include <vector>

void* printHello(void* arg)
{
    std::cout << "Hello";
    return arg;
}

void* printSpace(void* arg)
{
    std::cout << " ";
    return arg;
}

void* printWorld(void* arg)
{
    std::cout << "World";
    return arg;
}

int main()
{
    std::vector<void *( *)(void *)> t_list;
    t_list.push_back(printHello);
    t_list.push_back(printSpace);
    t_list.push_back(printWorld);
    
    // This prints out "Hello World"
    for(const auto& f : t_list)
    {
        f(nullptr);
    }

    return 0;
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top