function-pointers

correct way to assign function pointer

大兔子大兔子 提交于 2019-11-29 01:56:37
I'm a little confused about the correct syntax for assigning a function pointer to a variable. If I have a function foo int foo(); and I am assigning a pointer to foo to variable bar void * bar; it does not seem to matter if I use bar = foo; // or bar = &foo; It seems to me that only one of these should be correct or am I missing something? foo and &foo values are equivalent in C and have same type. The & operator here is correct but redundant. Note that assigning a function pointer to a void * is not valid in C. void *fp1 = foo; // invalid int (*fp2)() = foo; // valid int (*fp3)() = &foo; //

Cast to function pointer?

送分小仙女□ 提交于 2019-11-29 01:50:16
问题 Have come across the line of code shown below I think it may be a cast to a function pointer that returns void and takes a void pointer, is that correct? (void (*)(void *))SGENT_1_calc 回答1: Yes it is correct. I find that not very readable, so I suggest declaring the signature of the function to be pointed: typedef void sigrout_t(void*); I also have the coding convention that types ending with rout_t are such types for functions signatures. You might name it otherwise, since _t is a suffix

Function pointers/delegates in Java?

强颜欢笑 提交于 2019-11-29 01:37:46
For my Java game server I send the Action ID of the packet which basically tells the server what the packet is for. I want to map each Action ID (an integer) to a function. Is there a way of doing this without using a switch? What about this one? HashMap<Integer, Runnable> map = new HashMap<Integer, Runnable>(); map.put(Register.ID, new Runnable() { public void run() { functionA(); } }); map.put(NotifyMessage.ID, new Runnable() { public void run() { functionB(); } }); // ... map.get(id).run(); (If you need to pass some arguments, define your own interface with a function having a suitable

C++ How to Reference Templated Functions using std::bind / std::function

我与影子孤独终老i 提交于 2019-11-29 00:59:08
问题 If you have a templated class or a templated function, (or combination of the two), how do you bind that function, (preserving the template type parameter)? I was given some help about the basic syntax in a post below, to bind to functions with explicit template type parameters, but lose the ability to provide template type parameters in the process. Is it possible to get this to work so that it is still possible to provide template type parameters with future calls? Cleaned up this code a

Python, how to pass an argument to a function pointer parameter?

核能气质少年 提交于 2019-11-29 00:56:41
I only just started learning Python and found out that I can pass a function as the parameter of another function. Now if I call foo(bar()) it will not pass as a function pointer but the return value of the used function. Calling foo(bar) will pass the function, but this way I am not able to pass any additional arguments. What if I want to pass a function pointer that calls bar(42) ? I want the ability to repeat a function regardless of what arguments I have passed to it. def repeat(function, times): for calls in range(times): function() def foo(s): print s repeat(foo("test"), 4) In this case

Function Pointer in C

十年热恋 提交于 2019-11-29 00:43:18
How can I create a "function pointer" (and (for example) the function has parameters) in C? http://www.newty.de/fpt/index.html typedef int (*MathFunc)(int, int); int Add (int a, int b) { printf ("Add %d %d\n", a, b); return a + b; } int Subtract (int a, int b) { printf ("Subtract %d %d\n", a, b); return a - b; } int Perform (int a, int b, MathFunc f) { return f (a, b); } int main() { printf ("(10 + 2) - 6 = %d\n", Perform (Perform(10, 2, Add), 6, Subtract)); return 0; } typedef int (*funcptr)(int a, float b); funcptr x = some_func; int a = 3; float b = 4.3; x(a, b); I found this site helpful

How to read so many stars and parentheses in a templated function-pointer declaration? [duplicate]

左心房为你撑大大i 提交于 2019-11-28 21:37:46
This question already has an answer here: Complex C declaration 7 answers From Introduction to the C++11 feature: trailing return types The article claims template <class T> class tmp { public: int i; }; auto foo()->auto(*)()->tmp<int>(*)(){ return 0; } is equivalent to template <class T> class tmp{ public: int i; }; tmp<int> (*(*foo())())() { return 0; } I don't understand the complex function in the second code example. Where should I look at in the beginning? I guess is foo . But the stat right next to foo is going to define foo as a pointer... Based on the first code example, I will

Is the asterisk optional when calling a function pointer?

江枫思渺然 提交于 2019-11-28 21:18:06
I couldn't find an answer to this anywhere. I just read K&R and saw them calling a function pointer like this: (*ptr)(arg1, arg2); I vividly remember, however, to have seen someone using them like this: ptr(arg1, arg2); That might have been in C++, though. How are the rules? Do they differ in C and C++? TL;DR The rules in C and C++ are the same, there's no difference between the two. What does the C++ Standard ( n3797 ) say? 5.2.2p1 Function call [expr.call] A function call is a postfix expression followed by parentheses containing a possible empty, comma-separated list of initializer-clauses

How do I store a function to a variable?

左心房为你撑大大i 提交于 2019-11-28 20:40:13
I think they are called functors? (it's been a while) Basically, I want to store a pointer to a function in a variable, so I can specify what function I want to use from the command line. all the functions return and take the same values. unsigned int func_1 (unsigned int var1) unsigned int func_2 (unsigned int var1) function_pointer = either of the above? so then I could call it by going: function_pointer(my_variable)? EDIT: as per @larsmans's suggestion, I've gotten this: Config.h: class Config { public: unsigned static int (*current_hash_function)(unsigned int); }; Config.cpp: #include

Linux Kernel - why a function's address in System.map is one byte preceding its address as seen in real time?

风格不统一 提交于 2019-11-28 19:37:53
In linux kernel source code, added this lines in tasklet_action code: printk("tasklet_action = %p\n" , *tasklet_action); printk("tasklet_action = %p\n" , &tasklet_action); printk("tasklet_action = %p\n" , tasklet_action); In the output I get: tasklet_action = c03441a1 tasklet_action = c03441a1 tasklet_action = c03441a1 But when searching it in the system.map file the tasklet_action address is at c03441a0 so there is an offset of 1 byte. Why do I have this offset? Is it always an one byte offset? My guess is that you are running on ARM in Thumb mode , or on some other architecture that uses the