function-pointers

Procedure Pointer, Derived Type

浪子不回头ぞ 提交于 2019-11-30 20:43:49
The following doesnt compile in Intel Fortran XE 2011: TYPE type1 procedure(interface1),POINTER::p END TYPE type1 ABSTRACT INTERFACE integer function interface1(a) real,intent(in)::a END function interface1 END INTERFACE The error: error #8262: The passed-object dummy argument must be dummy data object with the same declared type as the type being defined. Add the nopass attribute to the declaration of the procedure pointer component. procedure(interface1), pointer, nopass :: p Edit: In response to your comment, if you want to use the pass keyword, the interface would have to be changed as

Dereferencing function pointers in C to access CODE memory

橙三吉。 提交于 2019-11-30 20:21:41
We are dealing with C here. I'm just had this idea, wondering if it is possible to access the point in memory where a function is stored, say foo and copying the contents of the function to another point in memory. Specifically, I'm trying to get the following to work: #include <stdlib.h> #include <stdio.h> #include <string.h> void foo(){ printf("Hello World"); } int main(){ void (*bar)(void) = malloc(sizeof foo); memcpy(&bar, &foo, sizeof foo); bar(); return 0; } But running it gives a bus error: Bus error: 10 . I'm trying to copy over the contents of function foo into a space of memory bar

How to properly define a function pointer in struct, which takes struct as a pointer?

情到浓时终转凉″ 提交于 2019-11-30 20:06:01
I have a struct with a callback function, the callback function needs a pointer to the structure in order to do its operation. How do I properly define these elements such that is will compile without warnings? typedef struct { // some fields required for processing... int (*doAction)(struct pr_PendingResponseItem *pr); } pr_PendingResponseItem; If I remove the "struct" attribute on the pr parameter, I get an error. If I leave it in, I get a warning: "its scope is only this definition or declaration, which is probably not what you want" It all works, but I would like to know the proper way to

How do function pointers work?

泄露秘密 提交于 2019-11-30 19:38:51
I'm asking some specific questions. How can I initialize them in a class? How can I pass a function as an argument? Do function pointers need to be declared and defined in the class? For question number 2 here is what I mean: void s(void) { //... } void f(function) { // what should I put as type to pass a function as an argument //... } f(s); To define a function pointer, use the following syntax: return_type (*ref_name) (type args, ...) So, to define a function reference named "doSomething", which returns an int and takes in an int argument, you'd write this: int (*doSomething)(int number);

How to create map<string, class::method> in c++ and be able to search for function and call it?

て烟熏妆下的殇ゞ 提交于 2019-11-30 17:39:32
I'm trying to create a map of string and method in C++, but I don't know how to do it. I would like to do something like that (pseudocode): map<string, method> mapping = { "sin", Math::sinFunc, "cos", Math::cosFunc, ... }; ... string &function; handler = mapping.find(function); int result; if (handler != NULL) result = (int) handler(20); To be honest I don't know is it possible in C++. I would like to have a map of string, method and be able to search for function in my mapping. If given string name of function exists then I would like to call it with given param. Well, I'm not a member of the

Function pointers with default parameters in C++

二次信任 提交于 2019-11-30 16:49:03
How does C++ handle function pointers in relation to functions with defaulted parameters? If I have: void foo(int i, float f = 0.0f); void bar(int i, float f); void (*func_ptr1)(int); void (*func_ptr2)(int, float); void (*func_ptr3)(int, float = 10.0f); Which function pointers can I use in relation to which function? Both foo() and bar() can only be assigned to func_ptr2 . §8.3.6/2 : A default argument is not part of the type of a function. [Example: int f(int = 0); void h() { int j = f(1); int k = f(); // OK, means f(0) } int (*p1)(int) = &f; int (*p2)() = &f; // error: type mismatch --end

How to create map<string, class::method> in c++ and be able to search for function and call it?

↘锁芯ラ 提交于 2019-11-30 16:47:18
问题 I'm trying to create a map of string and method in C++, but I don't know how to do it. I would like to do something like that (pseudocode): map<string, method> mapping = { "sin", Math::sinFunc, "cos", Math::cosFunc, ... }; ... string &function; handler = mapping.find(function); int result; if (handler != NULL) result = (int) handler(20); To be honest I don't know is it possible in C++. I would like to have a map of string, method and be able to search for function in my mapping. If given

function pointers generate 'invalid use of non-static member function' error

﹥>﹥吖頭↗ 提交于 2019-11-30 14:04:38
问题 I am trying to grasp pointer function concept in a better way. So I have a very simple and working example as: #include <iostream> using namespace std; int add(int first, int second) { return first + second; } int subtract(int first, int second) { return first - second; } int operation(int first, int second, int (*functocall)(int, int)) { return (*functocall)(first, second); } int main() { int a, b; int (*plus)(int, int); int (*minus)(int, int); plus = &add; minus = &subtract; a = operation(7

typedef int (*pf) needs explaining

笑着哭i 提交于 2019-11-30 13:48:33
问题 Generally, we use typedef to get alternate names for datatypes. For example -- typedef long int li; // li can be used now in place of long int But, what does the below typedef do? typedef int (*pf) (int, int); 回答1: typedef int (*pf) (int, int); This means that variables declared with the pf type are pointers to a function which takes two int parameters and returns an int . In other words, you can do something like this: #include <stdio.h> typedef int (*pf)(int,int); int addUp (int a, int b) {

How can I create function pointers from a string input in MATLAB?

跟風遠走 提交于 2019-11-30 13:10:25
If I use the inline function in MATLAB I can create a single function name that could respond differently depending on previous choices: if (someCondition) p = inline('a - b','a','b'); else p = inline('a + b','a','b'); end c = p(1,2); d = p(3,4); But the inline functions I'm creating are becoming quite epic, so I'd like to change them to other types of functions (i.e. m-files, subfunctions, or nested functions). Let's say I have m-files like Mercator.m , KavrayskiyVII.m , etc. (all taking a value for phi and lambda ), and I'd like to assign the chosen function to p in the same way as I have