So I'm doing an assignment where we need to pass functions we've made ourselves into a library provided for us.
Tree * createBinTree(int (*comparePointer) (TreeDataTypePtr data1,
TreeDataTypePtr data2), void (*destroyPointer) (TreeDataTypePtr));
int(*compStr)(void *, void *) = &compareName;
int compareName(void * rest1, void * rest2);
Tree * myTree = createBinTree((compstr)(str1,str2),die(str1));
You need to pass the function, not call the function and pass the return value:
Tree* myTree = createBinTree( compstr , die );
But this is still not correct. The types of
die must be compatible with the parameter types of the function
int(*compStr)( TreeDataTypePtr , TreeDataTypePtr ) = &compareName;
in which case,
compareName must be a function of the same type. The same goes for
Note that only casting function pointers to the compatible type will cause undefined behavior when they are used to call the function, the original type of the function pointer must be compatible with the type used to call the function.