initialize struct with function pointer

Viewed 41

I want initialize this way, mostly I missed somethong because I have error

the errors with assignment and with the struct


typedef struct handler
{
    void *data;
    int (*print)(void *);
    void (*add)(void *, int);
    int (*free_heap)(void *);
} handler;


void MultiArrayElemnts()
{
    handler arr[3]={0};

    *(int*)&arr[0].data = 3;
    arr[0].print = PrintInt;
    arr[0].add = AddInt;
    arr[0].free_heap = FreeDummy;

    *(float*)&arr[1].data = 3.3;
    arr[1].print = PrintFloat;
    arr[1].add = AddFloat;
    arr[1].free_heap = FreeDummy;

    *(char**)&arr[2].data = "Three";
    arr[2].print = PrintString;
    arr[2].add = AddString;
    arr[2].free_heap = FreeHeap;
} 

0 Answers
Related