function-pointers

C - Variadic macro which expands into set of macro calls on each argument

不问归期 提交于 2019-12-07 09:56:43
问题 I want to have a single macro call which takes in multiple function pointers, and each function pointer is called by a second macro which is a function declaration. I want two macros on the form #define FUNCTION_DEF(func) extern int func(void); #define FUNCTION_DEFS(...) (???) which is called as such FUNCTION_DEFS( myFunc1, myFunc2, otherFunc1, otherFunc2, defaultFunc ) which expands into FUNCTION_DEF(myFunc1) FUNCTION_DEF(myFunc2) FUNCTION_DEF(otherFunc1) FUNCTION_DEF(otherFunc2) FUNCTION

Why can function pointers be used with or without the address of operator? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-07 08:28:50
问题 This question already has answers here : Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'? (2 answers) Closed 10 months ago . In the book, "Beginning C from Novice to Professional", the author does not use the address of operator when assigning a function to a function pointer. I typed in the code on my compiler both with and without the address of operator and it compiled and performed as expected both times. Why is this and which way would be

Function pointer with undetermined parameter

夙愿已清 提交于 2019-12-07 07:25:12
问题 I want to have a function pointer that can take various types of parameters. How do I do that? The following example (line 1), I want void (*fp)(int) to be able to take void (*fp)(char*) as well. The following code does not properly compile because I'm passing char* where int is expected so compiling the following code will give you warnings (and won't work properly). void callIt(void (*fp)(int)) { (*fp)(5); } void intPrint(int x) { printf("%d\n", x); } void stringPrint(char *s) { printf("%s

Not allowed to return a function from a function. How could I?

不问归期 提交于 2019-12-07 07:11:26
问题 8.3.5/8 Functions [dcl.fct] says [...] Functions shall not have a return type of type array or function , although they may have a return type of type pointer or reference to such things. [...] Why so explicit of a rule? Is there some syntax that would even allow returning a function as opposed to a function pointer? Am I miss-interpreting the quote? typedef void (*fp)(); void foo(){} fp goo() { return foo; //automatically converted to function pointer } 回答1: This is quite a contrived example

warning: assignment from incompatible pointer type [enabled by default] while assigning function address to function pointer

空扰寡人 提交于 2019-12-07 07:10:09
问题 I am trying to implement a simple swap function using function pointer but when I assign function's address to a function pointer am getting pointersTofunctionB.c:14:6:warning: assignment from incompatible pointer type [enabled by default] . Below is my code #include <stdio.h> void intSwap(int *a,int *b); void charSwap(char *a,char *b); void (*swap)(void*,void*); int main(int argc, char const *argv[]) { int a=20,b=15; char c='j',d='H'; swap=&intSwap;// warning here swap(&a,&b); printf("%d %d

Pass a c++ lambda to old c function pointer

喜你入骨 提交于 2019-12-07 06:59:55
问题 I have to create a c++ wrapper to a old c library. In a class method I must call a c function that takes with other stuff also a function pointer(it is a event handler and the function takes a function that is fired when a event happens). A simple example of it is this: void myclass::add_handler(std::function<void()> handler, otherstuff...) { /* * Many things. */ type_of_function_pointer_accepted_by_old_c_library_add_handler nameofvariable = [handler](same_arguments_as_in_definition_of_the

Setting up a array of callbacks and trying to use array index as value in callback

走远了吗. 提交于 2019-12-07 06:35:18
问题 When I setup an array of callbacks this way I get 20 in the dialog window for all callbacks. I'd like to get the index in the array instead. Is this possible? The function that calls the callback is expecting the callback to have one parameter. I don't control the caller of the callback because it is part of an external library. Any help is appreciated. for (var i = 0; i < 20; i++) { callbackDB[i] = function(data) { alert(i); } } 回答1: Because i is evaluated when the function is called, you'll

Is it possible to create a function pointer to the a function's `new` operator/constructor?

僤鯓⒐⒋嵵緔 提交于 2019-12-07 06:14:02
问题 If I were to wanted to parameterize creating an object, I could of course make a function which called new on a particular class and passed out a pointer. I am wondering if it's possible to skip that step and pass a function pointer to the new operator itself. 回答1: boost::lambda provides function wrappers for new and delete. These can be used to easily convert an new call into a function object. 回答2: operator new (as well as the other flavours) takes care of allocating memory but does not

Executing Byte Array in Go

醉酒当歌 提交于 2019-12-07 05:58:13
问题 I'm trying to execute shellcode within a Go program, similar to how you can do it with other languages. Example 1 - Shellcode in C program Example 2 - http://www.debasish.in/2012/04/execute-shellcode-using-python.html All methods have broadly similar techniques - assign the shellcode to executable memory via the OS specific allocation (mmap, virtualalloc, etc) and then execute the code by creating a function pointer pointing to that location before executing. Here is my horrible hacky example

Conditional operator can't resolve overloaded member function pointers

岁酱吖の 提交于 2019-12-07 05:35:37
问题 I'm having a minor issue dealing with pointers to overloaded member functions in C++. The following code compiles fine: class Foo { public: float X() const; void X(const float x); float Y() const; void Y(const float y); }; void (Foo::*func)(const float) = &Foo::X; But this doesn't compile (the compiler complains that the overloads are ambiguous): void (Foo::*func)(const float) = (someCondition ? &Foo::X : &Foo::Y); Presumably this is something to do with the compiler sorting out the return