Is it possible to initialize (in below example) the function-pointer fp.p1 and fp.p2 by using a loop ? ... if yes, what should be coded at the first commentline. Thanks.
typedefbool (*point1)();
typedefshort (*point2)(long);
typedefstruct __functionPointers
{
point1 p1;
point2 p2;
} functionPointers;
int main(){
functionPointers fp;
short nr;
nr = sizeof(fp) / sizeof(fp.p1);
// here: define pointer to fp or fp.p1 , but how ?
for (int i=0; i<nr; i++) {
// here: set to NULL
// here ++ the defined pointer
}
}
To be pedantic, that works on most common platforms, but is not a valid answer in general: a null pointer is not always zero when reinterpreted.
To value-initialize every member of the struct (which initializes pointers to correct null pointer values), simply value-initialize your struct:
1 2 3 4
functionPointers fp = {NULL}; // in C
functionPointers fp = {}; // in C++ (but in C++, you wouldn't use that typedef)
functionPointers fp = functionPointers(); // also C++
functionPointers fp{}; // in C++11