function-pointers

Function pointers and address of a function

限于喜欢 提交于 2019-11-26 03:45:36
So I figured when making function pointers, you do not need the operator & to get the address of the initial function: #include <stdio.h> double foo (double x){ return x*x; } int main () { double (*fun1)(double) = &foo; double (*fun2)(double) = foo; printf("%f\n",fun1(10)); printf("%f\n",fun2(10)); printf("fun1 = %p \t &foo = %p\n",fun1, &foo); printf("fun2 = %p \t foo = %p\n",fun2, foo); int a[10]; printf(" a = %p \n &a = %p \n",a,&a); return 0; } output: >./a.out 100.000000 100.000000 fun1 = 0x4004f4 &foo = 0x4004f4 fun2 = 0x4004f4 foo = 0x4004f4 a = 0x7fff26804470 &a = 0x7fff26804470 Then I

How to call through a member function pointer?

天涯浪子 提交于 2019-11-26 03:40:03
问题 I\'m trying to do some testing with member function pointer. What is wrong with this code? The bigCat.*pcat(); statement doesn\'t compile. class cat { public: void walk() { printf(\"cat is walking \\n\"); } }; int main(){ cat bigCat; void (cat::*pcat)(); pcat = &cat::walk; bigCat.*pcat(); } 回答1: More parentheses are required: (bigCat.*pcat)(); ^ ^ The function call ( () ) has higher precedence than the pointer-to-member binding operator ( .* ). The unary operators have higher precedence than

C function pointer casting to void pointer

匆匆过客 提交于 2019-11-26 03:35:20
问题 I am trying to run the following program but getting some strange errors: File 1.c: typedef unsigned long (*FN_GET_VAL)(void); FN_GET_VAL gfnPtr; void setCallback(const void *fnPointer) { gfnPtr = *((FN_GET_VAL*) (&fnPointer)); } File 2.c: extern FN_GET_VAL gfnPtr; unsigned long myfunc(void) { return 0; } main() { setCallback((void*)myfunc); gfnPtr(); /* Crashing as value was not properly assigned in setCallback function */ } Here the gfnPtr() is crashing on 64-Bit suse linux when compiled

Callback functions in C++

狂风中的少年 提交于 2019-11-26 03:31:14
问题 In C++, when and how do you use a callback function? EDIT: I would like to see a simple example to write a callback function. 回答1: Note: Most of the answers cover function pointers which is one possibility to achieve "callback" logic in C++, but as of today not the most favourable one I think. What are callbacks(?) and why to use them(!) A callback is a callable (see further down) accepted by a class or function, used to customize the current logic depending on that callback. One reason to

How to format a function pointer?

可紊 提交于 2019-11-26 02:57:17
问题 Is there any way to print a pointer to a function in ANSI C? Of course this means you have to cast the function pointer to void pointer, but it appears that\'s not possible?? #include <stdio.h> int main() { int (*funcptr)() = main; printf(\"%p\\n\", (void* )funcptr); printf(\"%p\\n\", (void* )main); return 0; } $ gcc -ansi -pedantic -Wall test.c -o test test.c: In function \'main\': test.c:6: warning: ISO C forbids conversion of function pointer to object pointer type test.c:7: warning: ISO C

c++/cli pass (managed) delegate to unmanaged code

故事扮演 提交于 2019-11-26 02:55:16
问题 How do I pass a function pointer from managed C++ (C++/CLI) to an unmanaged method? I read a few articles, like this one from MSDN, but it describes two different assemblies, while I want only one. Here is my code: 1) Header (MyInterop.ManagedCppLib.h): #pragma once using namespace System; namespace MyInterop { namespace ManagedCppLib { public ref class MyManagedClass { public: void DoSomething(); }; }} 2) CPP Code (MyInterop.ManagedCppLib.cpp) #include \"stdafx.h\" #include \"MyInterop

Do distinct functions have distinct addresses?

Deadly 提交于 2019-11-26 02:20:33
问题 Consider these two functions: void foo() {} void bar() {} is it guaranteed that &foo != &bar ? Similarly, template<class T> void foo() { } is it guaranteed that &foo<int> != &foo<double> ? There are two linkers I know of that fold function definitions together. MSVC aggressively COMDAT folds functions, so two functions with the same implementation can be turned into one function. As a side effect, the two functions share the same address. I was under the impression that this was illegal, but

Why are function pointers and data pointers incompatible in C/C++?

心已入冬 提交于 2019-11-26 02:07:51
问题 I have read that converting a function pointer to a data pointer and vice versa works on most platforms but is not guaranteed to work. Why is this the case? Shouldn\'t both be simply addresses into main memory and therefore be compatible? 回答1: An architecture doesn't have to store code and data in the same memory. With a Harvard architecture, code and data are stored in completely different memory. Most architectures are Von Neumann architectures with code and data in the same memory but C

How can I use an array of function pointers?

老子叫甜甜 提交于 2019-11-26 01:39:18
问题 How should I use array of function pointers in C? How can I initialize them? 回答1: You have a good example here (Array of Function pointers), with the syntax detailed. int sum(int a, int b); int subtract(int a, int b); int mul(int a, int b); int div(int a, int b); int (*p[4]) (int x, int y); int main(void) { int result; int i, j, op; p[0] = sum; /* address of sum() */ p[1] = subtract; /* address of subtract() */ p[2] = mul; /* address of mul() */ p[3] = div; /* address of div() */ [...] To

C++ lambda with captures as a function pointer

百般思念 提交于 2019-11-26 01:34:10
问题 I was playing with C++ lambdas and their implicit conversion to function pointers. My starting example was using them as callback for the ftw function. This works as expected. #include <ftw.h> #include <iostream> using namespace std; int main() { auto callback = [](const char *fpath, const struct stat *sb, int typeflag) -> int { cout << fpath << endl; return 0; }; int ret = ftw(\"/etc\", callback, 1); return ret; } After modifying it to use captures: int main() { vector<string> entries; auto