I have a struct that contains a declaration like this one:
void (*functions[256])(void) //Array of 256 functions without arguments and return value
And in another function I want to define it, but there are 256 functions!
I could do something like this:
struct.functions[0] = function0;
struct.functions[1] = function1;
struct.functions[2] = function2;
And so on, but this is too tiring, my question is there some way to do something like this?
struct.functions = { function0, function1, function2, function3, ..., };
EDIT: Syntax error corrected as said by Chris Lutz.
No you don’t. That’s a syntax error. You’re looking for:
Which is an array of function pointers. Note, however, that
void func()isn’t a “function that takes no arguments and returns nothing.” It is a function that takes unspecified numbers or types of arguments and returns nothing. If you want “no arguments” you need this:In C++,
void func()does mean “takes no arguments,” which causes some confusion (especially since the functionality C specifies forvoid func()is of dubious value.)Either way, you should
typedefyour function pointer. It’ll make the code infinitely easier to understand, and you’ll only have one chance (at thetypedef) to get the syntax wrong:Anyway, you can’t assign to an array, but you can initialize an array and copy the data: