function-pointers

How to call functions by their pointers passing multiple arguments in C?

半世苍凉 提交于 2019-12-18 09:25:31
问题 I need to make a "function caller" function: it receives a generic function pointer ( void * ) and a variable number of arguments as arguments and it's got to call this function, passing the arguments, and return a generic pointer to the returning value. However, this entry-function pointer may point to any kind of function (with any returning type), even to functions with a constant number of arguments. It would be something like: void * function_caller(void * function_pointer, ...) { void *

Python ctypes to return an array of function pointers

感情迁移 提交于 2019-12-18 08:40:22
问题 I am working with a .dll that contains a single call which returns an array of function pointers. GetMyApi() returns a pointer to a struct, which is an array of function pointers. The functions themselves have different individual inputs and outputs. What I have tried so far: C code that I can't easily alter: Code in C : typedef struct My_Api_V2 { int (__cdecl *IsValidInt)(int i); int (__cdecl *InvalidInt)(); int (__cdecl *IsValidSize)(size_t i); } my_Api_V2; const my_Api_V2* GetMyApi(int

Why must I use address-of operator to get a pointer to a member function?

牧云@^-^@ 提交于 2019-12-18 08:35:43
问题 struct A { void f() {} }; void f() {} int main() { auto p1 = &f; // ok auto p2 = f; // ok auto p3 = &A::f; // ok // // error : call to non-static member function // without an object argument // auto p4 = A::f; // Why not ok? } Why must I use address-of operator to get a pointer to a member function? 回答1: auto p1 = &f; // ok auto p2 = f; // ok The first is more or less the right thing. But because non-member functions have implicit conversions to pointers, the & isn't necessary. C++ makes

Why must I use address-of operator to get a pointer to a member function?

雨燕双飞 提交于 2019-12-18 08:35:27
问题 struct A { void f() {} }; void f() {} int main() { auto p1 = &f; // ok auto p2 = f; // ok auto p3 = &A::f; // ok // // error : call to non-static member function // without an object argument // auto p4 = A::f; // Why not ok? } Why must I use address-of operator to get a pointer to a member function? 回答1: auto p1 = &f; // ok auto p2 = f; // ok The first is more or less the right thing. But because non-member functions have implicit conversions to pointers, the & isn't necessary. C++ makes

Assign C++ instance method to a global-function-pointer?

喜欢而已 提交于 2019-12-18 06:58:24
问题 Greetings, My project structure is as follows: \- base (C static library) callbacks.h callbacks.c paint_node.c . . * libBase.a \-app (C++ application) main.cpp In C library 'base' , I have declared global-function-pointer as: in singleheader file callbacks.h #ifndef CALLBACKS_H_ #define CALLBACKS_H_ extern void (*putPixelCallBack)(); extern void (*putImageCallBack)(); #endif /* CALLBACKS_H_ */ in single C file they are initialized as callbacks.c #include "callbacks.h" void (*putPixelCallBack)

Calling a function through a function pointer - dereference the pointer or not? What's the difference?

百般思念 提交于 2019-12-18 05:57:36
问题 I tried both - C and C++ and both work fine. I'm kinda new to function pointers and here's a simple code, that surprised me: #include <assert.h> void sort( int* arr, const int N ); int main () { int arr1[] = { 1, 5, 2, 6, 2 }; int arr2[] = { 1, 5, 2, 6, 2 }; void (*sort_ptr)( int*, const int) = sort; sort_ptr( arr1, 5 ); (*sort_ptr)( arr2, 5 ); assert( arr1[0] == 1 && arr1[1] == 2 && arr1[2] == 2 && arr1[3] == 5 && arr1[4] == 6 ); assert( arr2[0] == 1 && arr2[1] == 2 && arr2[2] == 2 && arr2[3

How can I typedef a function pointer that takes a function of its own type as an argument?

↘锁芯ラ 提交于 2019-12-18 05:45:11
问题 Example: A function that takes a function (that takes a function (that ...) and an int) and an int. typedef void(*Func)(void (*)(void (*)(...), int), int); It explodes recursively where (...) . Is there a fundamental reason this can't be done or is there another syntax? It seems to me it should be possible without a cast. I'm really trying to pass a dispatch-table but I could figure that out if I could just pass this one type. 回答1: You can wrap the function pointer in a struct: struct fnptr

Default value of a function pointer in C++

纵饮孤独 提交于 2019-12-18 05:03:53
问题 What is the default value of a function pointer in C++? (Apparently it can't be NULL , so what is it?) How is this program supposed to behave and why? struct S { void (*f)(); }; int main() { S s = S(); s.f(); // What is the value of s.f? } 回答1: In your case the object s is zero-initialized which means the function pointer is NULL . struct S { void (*f)(); }; int main() { S s = S(); if ( s.f == NULL) std::cout << "s.f is NULL" << std::endl; } Output: s.f is NULL Online demo. 回答2: First any

Default value of a function pointer in C++

牧云@^-^@ 提交于 2019-12-18 05:03:45
问题 What is the default value of a function pointer in C++? (Apparently it can't be NULL , so what is it?) How is this program supposed to behave and why? struct S { void (*f)(); }; int main() { S s = S(); s.f(); // What is the value of s.f? } 回答1: In your case the object s is zero-initialized which means the function pointer is NULL . struct S { void (*f)(); }; int main() { S s = S(); if ( s.f == NULL) std::cout << "s.f is NULL" << std::endl; } Output: s.f is NULL Online demo. 回答2: First any

C++ detecting free function existence with explicit parameters

谁说我不能喝 提交于 2019-12-18 04:11:08
问题 I'm writing some type traits to see if a free function exists with a specific set of parameters. The functions have a signature that looks something like this: template <class T> void func( SomeClass &, SomeType const & ); I know ahead of time the values for T , SomeClass , and SomeType . I want the trait to return true if this function exists with exactly these parameters, not using any implicit conversion. I can easily write some code to detect whether this function exists by using SFINAE