function-pointers

Is there an alternate syntax to typedef function pointers?

最后都变了- 提交于 2019-12-01 02:52:39
问题 For doing a typedef for a function pointer, we do something like this, typedef int (*func) (char*); typedef struct{ char * name; func f1; } As opposed to this, I came across a code, which I don't understand. typedef int rl_icpfunc_t (char *); typedef struct { char *name; /* User printable name of the function. */ rl_icpfunc_t *func; /* Function to call to do the job. */ char *doc; /* Documentation for this function. */ }COMMAND; This is a code snippet from an example of the libedit library.

How do you declare a const array of function pointers?

混江龙づ霸主 提交于 2019-12-01 02:46:00
Firstly, I've got functions like this. void func1(); void func2(); void func3(); Then I create my typedef for the array: void (*FP)(); If I write a normal array of function pointers, it should be something like this: FP array[3] = {&func1, &func2, &func3}; I want to make it a constant array, using const before "FP", but I've got this error messages: error: cannot convert 'void ( * )()' to 'void ( * const)()' inialization PD: Sorry my bad English. EDIT: x.h typedef void (*FP)(); class x { private: int number; void func1(); void func2(); void func3(); static const FP array[3]; } x.cpp const FP x

address of c++ template function

我们两清 提交于 2019-12-01 02:17:20
Why does this fail to compile? (g++-4.5) template < typename U > static void h () { } int main () { auto p = &h<int>; // error: p has incomplete type } EDIT : Here is a work-around: template < typename U > static void h () { } int main () { typedef decltype (&h<int>) D; D p = &h<int>; // works } In C++0x this is guaranteed to work. However in C++03 this wasn't working (the initializer part, that is) and some compilers apparently don't support it yet. Furthermore, I remember that the C++0x wording is not clear what happens with &h<int> when it is an argument to a function template and the

What does a pointer initialise to?

烈酒焚心 提交于 2019-12-01 02:08:57
问题 One thing that always confused me , the character pointer. It's after long four years that I am again lingering into c. Take for example the mentioned case .Why does char pointer behave in this way ? How can we directly address the contents of the pointee when it points to nothing or is it like char pointer stores stuffs other than addresses ! #include <stdio.h> #include <stdlib.h> int main() { char* charPtr="I cant understand why"; int* intPtr=60; printf("%d\n", intPtr); //displays 60 printf

Visual C++ ~ Not inlining simple const function pointer calls

懵懂的女人 提交于 2019-12-01 00:56:11
问题 Dear StackOverflowers, I got a simple piece of code which I am compiling on Microsoft Visual Studio C++ 2012: int add(int x, int y) { return x + y; } typedef int (*func_t)(int, int); class A { public: const static func_t FP; }; const func_t A::FP = &add; int main() { int x = 3; int y = 2; int z = A::FP(x, y); return 0; } The compiler generates the following code: int main() { 000000013FBA2430 sub rsp,28h int x = 3; int y = 2; int z = A::FP(x, y); 000000013FBA2434 mov edx,2 000000013FBA2439

Calling local functions from command line

喜夏-厌秋 提交于 2019-12-01 00:47:08
问题 I have a local function defined in an m-file. For example: % begining of public_function.m file function fh = public_function( ) % % do some computation... fh = @local_function; % return function handle to local function defined below function y = local_function( x ) % % a local function inside public_function.m file % % some manipulation on x y = x; % end of public_function.m file NOTE THAT local_function is NOT nested Now, I would like to call local_function from command line (and not from

Is it possible to swap C functions?

痴心易碎 提交于 2019-11-30 22:34:54
Looking to see if anyone knows if its possible to swap C functions...? void swap2(int(*a)(int), int(*b)(int)) { int(*temp)(int) = a; *a = *b; *b = temp; // Gives 'Non-object type 'int (int)' is not assignable } swap2(&funcA, &funcB); EDIT More data here as to intention -- Some answers have been provided below which do work such as creating the function ptr using typedef , pointing them to the functions and switching those, which lets you invoke the new swapped ptrs successfully. BUT calling the functions by their original names after swapping shows no change. Essentially I'm looking for a c

address of c++ template function

霸气de小男生 提交于 2019-11-30 21:47:05
问题 Why does this fail to compile? (g++-4.5) template < typename U > static void h () { } int main () { auto p = &h<int>; // error: p has incomplete type } EDIT : Here is a work-around: template < typename U > static void h () { } int main () { typedef decltype (&h<int>) D; D p = &h<int>; // works } 回答1: In C++0x this is guaranteed to work. However in C++03 this wasn't working (the initializer part, that is) and some compilers apparently don't support it yet. Furthermore, I remember that the C+

Is passing additional parameters through function pointer legal/defined in C? [duplicate]

我的未来我决定 提交于 2019-11-30 21:23:50
Possible Duplicate: Casting a function pointer to another type Assume i initialize a function pointer with a function that actually takes less parameters then the function pointer definition, will the function still perform correctly if called through the function pointer? I tried this with gcc and it worked as expected, but i wonder if that behaviour is consistent across compilers/platforms (i suspect in some enviroments it might wreak havoc on the stack): #include <stdio.h> typedef void (*myfun)(int, int, int); void test_a(int x, int y, int z) { printf("test_a %d %d %d\n", x, y, z); } void

C++ std::mem_fn with overloaded member function

◇◆丶佛笑我妖孽 提交于 2019-11-30 20:47:06
When compiling the following code, Visual Studio reports: \main.cpp(21): error C2664: 'std::_Call_wrapper<std::_Callable_pmd<int ClassA::* const ,_Arg0,false>,false> std::mem_fn<void,ClassA>(int ClassA::* const )' : cannot convert argument 1 from 'overloaded-function' to 'int ClassA::* const ' 1> with 1> [ 1> _Arg0=ClassA 1> ] 1> Context does not allow for disambiguation of overloaded function Why is the compiler confused when creating mem_fptr1 ? But some how mem_fptr2 is ok when I specify the types. Can I create member function pointer to an overloaded member function that takes no argument?