typedef struct foo{
void (*del)(void *toDel);
char* (*p)(void *tp)
} Foo;
Foo init(char* (*print)(void*),void (*delFunc)(void*));
Trying to figure out how to assign or initialize the supplied parameters to the struct function pointers.
Foo init(char* (*print)(void *toBePrinted),void (*delFunc)(void *toBeDeleted))
{
return Foo{ .del = delFunc, .p = print};
}
What about this? Long form:
Foo init(char* (*print)(void *toBePrinted),void (*delFunc)(void *toBeDeleted))
{
Foo tmp = {
.del = delFunc,
.p = print
};
return tmp;
}
Foo, the tmp variable would be copied to the caller. If, for example, caller executes Foo ptrs_to_fncs = init(ptr1, ptr2);, where ptr1 and ptr2 are appropriate pointers to functions, then the tmp will be copied to the ptrs_to_fncs and therefore no UB. Returning structs in C works similar to returning objects in C++ - they get copied. Of course, the function init() can be rewritten to use dynamically allocated memory instead.How to initialize a struct in accordance with C programming language standards
You can do it the usual way:
Return (Foo){.del=delFunc, .p=print};