function-pointers

Is the asterisk optional when calling a function pointer?

不打扰是莪最后的温柔 提交于 2019-12-20 10:37:47
问题 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++? 回答1: 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

What is the difference between delegate in c# and function pointer in c++? [duplicate]

痴心易碎 提交于 2019-12-20 10:07:10
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: are there function pointers in c#? I'm interested in finding the difference between delegate in C# and function pointer in C++. 回答1: A delegate in C# is a type-safe function pointer with a built in iterator. It's guaranteed to point to a valid function with the specified signature (unlike C where pointers can be cast to point to who knows what). It also supports the concept of iterating through multiple bound

Pass instance method as function pointer to C Library

拈花ヽ惹草 提交于 2019-12-20 09:48:50
问题 I am writing an Objective-C application that uses a C Library. The issue which i am currently facing is that the C Library has a structure where some field are function pointers later used as callbacks. How can i convert an Objective-C instance method to a function pointer and pass it to the library? 回答1: You will need to provide the C callback function within the Objective-C class implementation file, and this will only work if the callback is able to use a context pointer of some sort. So

Passing any function as template parameter

风流意气都作罢 提交于 2019-12-20 08:56:38
问题 I want to pass a function value as a template parameter to a function. Currently the best I managed to do is : template< typename F, F f > void pass() { ... } ...which is used: pass< decltype(&func), &func >(); What I would really like is to have: pass< &func >(); Is there any way to achieve this without macros? Basically to pass both the type and the value at the same time? The compiler obviously has all the information needed for that... The solution must work with variable parameters and

Meaning of int (*) (int *) = 5 (or any integer value)

可紊 提交于 2019-12-20 08:09:04
问题 I cannot figure this out: int main() { int (*) (int *) = 5; return 0; } The above assignment compiles with g++ c++11. I know that int (*) (int *) is a pointer to a function that accepts an (int *) as argument and returns an int, but I do not understand how you could equate it to 5. At first I thought it is a function that constantly returns 5 (from my recent learning in F#, probably, haha), then I thought, briefly, that the function pointer points to memory location 5, but that does not work,

How to pass arrays in the main func. w/ c++

蓝咒 提交于 2019-12-20 07:48:23
问题 #include <iostream> using namespace std; const int MAX = 1000; int ArrMix[MAX]; int *ptrArrPos[MAX]; int *ptrArrNeg[MAX]; int PosCounter = 0; int NegCounter = 0; int r; void accept(int ArrMix[MAX]) { cout<<"Enter the number of elements in your array: "; cin>>r; for (int i = 0; i < r; i++) { cout<<"Enter value:"; cin>>ArrMix[i]; } } void process(int &i) { if(ArrMix[i] >= 0) { ptrArrPos[PosCounter] = &ArrMix[i]; PosCounter++; } else { ptrArrNeg[NegCounter] = &ArrMix[i]; NegCounter++; } } void

Restart a computer using function pointers in C

拜拜、爱过 提交于 2019-12-20 07:18:20
问题 I was learning about function pointers in C when I came across a program that restarts the computer upon execution. void (*f) (void); f=(void (*)(void) MK_FP(0xFFFF,0x0000); f(); The (void (*)(void)) portion wasn't in the original text provided, and I had to make this addition to get the code to compile. How does this work? Thank you very much. 回答1: About programs that manually construct function pointers from numeric literals and call them, the C standard says only that the behavior is

How to dynamically allocate function code?

梦想的初衷 提交于 2019-12-20 06:23:23
问题 In the C language, the usual route for function pointers as callbacks from some library is to include a void* pointer for the context of the user: void (*fp)(void* ctx); The allows the library to call the callback with context ctx . Let's say I'm using a library that does not include a context pointer in the callbacks. I will need to have one callback for each context. What is the most portable way to dynamically allocate function pointers in C for providing callbacks? How can I malloc() a

C++ map of “events” and member function pointers

不羁的心 提交于 2019-12-20 06:21:28
问题 I've managed to write a template class to work like a callback, learned from the accepted answer of this question How to define a general member function pointer. I wish to have a map of string keys and callback values so that I can invoke the proper callback that matches a string. This would be fine but I need the map to support callbacks from different classes. Right now it can only work with one class. It can be any class because of the template but only a collection of callbacks from the

How to initialize array of pointers to functions?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 04:59:11
问题 I have following code: typedef int (*t_Function) (int x); t_Function Functions[MAX_FUNCTIONS]; int f(int x) { return 0; } But I cannot initialize it properly. If I add following line: Functions[0] = f; then compiler generates following error: prog.c:217: warning: data definition has no type or storage class prog.c:217: error: conflicting types for Functions How to initialize this array of pointers to functions? 回答1: You should either do it inside a function, where Functions[0] = f; works fine