function-pointers

What is the difference between pointer to array and pointer to pointer?

假装没事ソ 提交于 2019-12-06 13:41:35
I'm new in programming and learning about pointers in array. I'm a bit confused right now. Have a look at the program below: #include <stdio.h> int fun(); int main() { int num[3][3]={23,32,478,55,0,56,25,13, 80}; printf("%d\n",*(*(num+0)+1)); fun(num); printf("%d\n", *(*(num+0)+1)); *(*(num+0)+0)=23; printf("%d\n",*(*(num+0))); return 0; } int fun(*p) // Compilation error { *(p+0)=0; return 0; } This was the program written in my teacher's notes. Here in the main() function, in the printf() function dereference operator is being used two times because num is pointer to array so first time

Python function pointers within the same Class

二次信任 提交于 2019-12-06 13:15:12
I have the following class in Python that I am trying to use for calling a set of its own methods through a dictionary that has the pointers of the available functions: class Test(): functions = { 'operation_a' : Test.function_a; 'operation_b' : Test.function_b; } def run_operations(operation, *args, **kwargs): try: functions[str(operation)](self, args, kwargs) except KeyError: // some log ... def function_a(self, *args, **kwargs): print A def function_b(self, *args, **kwargs): print B This first approach seems to be incorrect since the Python interpreter cannot find class 'Test' (NameError:

Creating a vector of pointer to functions

旧时模样 提交于 2019-12-06 12:42:01
How do I do this in C++? I know how to create a pointer to a function but that requires having a name for that pointer. What I actually need is to somehow create a pointer without naming it. I know the syntax for array of ptr to functions. This might help: out-type (*ptr[size])(parameters...) You should really use a boost/std::function<out_type(parameters)> instead. However, to answer the question at hand, you could use a typedef typedef out_type(*typedef_name)(param_types); std::vector<typedef_name> vec; Or you can just supply the vector with the type directly. std::vector<out_type(*)(param

Polymorphism without virtual in C++ for multi level inheritance

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 11:50:01
I have a situation where I need to achieve polymorphism without vtable. Here is what I am trying to do There is a class hierarchy: C extends B, B extends A The idea is to declare a function pointer in A and constructors of B and C assign their corresponding methods to the function pointer in A With the code below I am able to achieve polymorphism for class C but not for class B. Obviously I am missing something here. I am not sure if this is even possible. Greatly appreciate any insights into this problem. I can do this with the below code A<C> *c = new C(); c->BasePrint(); //Reached C's Print

Why need to use “WINAPI*” for the Syntax for declaring function pointers for functions in a DLL

会有一股神秘感。 提交于 2019-12-06 11:37:43
I have a C++ console application & a DLL. In the C++ application I see the following snippet :: typedef DWORD (WINAPI* functABC)(unsigned long*); functABC functABC111; HMODULE handleDLL = LOadLibrary("a.DLL"); functABC111 = (functABC)GetProcAddress(handleDLL,"function_1"); At a high level I understand that we are getting the function pointer to the function in a.DLL "function_1()". But want to understand the 1st 2 lines in the above snippet :: typedef DWORD (WINAPI* functABC)(unsigned long*); functABC functABC111; 2 questions :: 1) Is the name "functABC" just a random function pointer name? 2)

Member function pointer runtime error - The value of ESP was not properly saved across a function call

。_饼干妹妹 提交于 2019-12-06 11:18:47
问题 I've been searching for answers to this problem for the past hour but can't find a solution that works. I'm trying to use function pointers to call a non-static member function of a specific object. My code compiles fine, but during runtime I get a nasty runtime exception that says: Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with

Create function in memory

*爱你&永不变心* 提交于 2019-12-06 09:50:35
I learned about function pointers recently in class and I was wondering if you could assign a function pointer to a block of memory allocated by a program, fill the memory block with assembly commands (hex values of op codes), then call the memory block using the function pointer. I don't know much about function pointers, but I'm guessing you can't assign them wherever you want in memory, they need to point to a function. If that's true, how can you create a function in memory to be called? Is that even possible? Here's some code I typed up to show the concept. I used various opcode values to

Call function pointer from JNI

人走茶凉 提交于 2019-12-06 08:59:45
问题 I have a function already implemented in cpp with prototype MyFunction(int size, int (* callback)(UINT16* arg1, UINT16* arg2)); Second argument is a function pointer which must be implemented in java. How can i implement that function? Also how can i call MyFunction in JNI? Please help 回答1: Try this Java import java.util.*; public class JNIExample{ static{ try{ System.loadLibrary("jnicpplib"); }catch(Exception e){ System.out.println(e.toString()); } } public native void SetCallback(JNIExample

function pointer handling in Doxygen in C

拥有回忆 提交于 2019-12-06 08:57:13
In my code, a vtable containts several function pointer. Doxygen is unable to follow them. I'd like to force it to recognize the possible paths, to produce a complete Call Graph, since now this part is missing. example: typedef Bool(*SPECIAL_FUNC)(KEY key); typedef struct{ int a; int b; SPECIAL_FUNC; }OBJ; Bool add(KEY key); //code... Bool sub(KEY key); //code... While this will likely not affect the call graph in doxygen, you can document the functions as related to the structure, and provide links to them in the documentation for SPECIAL_FUNC . Something like: typedef Bool(*SPECIAL_FUNC)(KEY

ANSI C: If a function pointer points to executable code does that mean less execution overhead than simply invoking the function? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-06 08:28:48
This question already has answers here : Does Function pointer make the program slow? (8 answers) Closed 2 years ago . We know that using function pointers in C can be quite helpful when used in the proper scenarios (calling a function at runtime vs compile time, making the code more readable, etc.), but there isn't much literature around simple function invocation vs using a function pointer. void foo(void) { printf("hello\n"); } int testFcn(void) { // simple invokation foo(); return 0; } // Or, declare function pointer and assign void (*myFunc)(void) = foo; int testFcn(myFunc) { // Function