How to initialize functionpointers via a pointer

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
typedef bool  (*point1)();
typedef short (*point2)(long);

typedef struct __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   
    }
}
if you cast it to an array then yes. But, well, don't

Instead use memset:

http://www.cplusplus.com/reference/clibrary/cstring/memset/

memset(&fp, 0, sizeof(fp));
oke ... works great ... case close ;-) ... thx.
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 


or provide a constructor.
Last edited on
Topic archived. No new replies allowed.