Address of function pointers in C++ -
this question has answer here:
- how print function pointers cout? 6 answers
i'm not clear on values being returning calling:
&next, fp, *fp, &return_func_ptr, fp_ptr, &fp_ptr, *fp_ptr
they seem give me value 1
. mean?
also, how declare
int (*return_f())(char)
to receive parameter without using typedef?
#include <iostream> int next(int n){ return n+99; } // returns pointer function typedef int (*fptr)(int); // using typdef fptr return_func_ptr(){ return next; } int f(char){ return 0; } int (*return_f())(char){ // how pass parameter here? // std::cout << "do " << param << std::endl; return f; } int main() { int x = 5; // p points x int *p = &x; std::cout << "x=" << x << std::endl; // 5, value of x std::cout << "&x=" << &x << std::endl; // 0x7fff6447a82c, address of x std::cout << "p=" << p << std::endl; // 0x7fff6447a82c, value of p address of x std::cout << "*p=" << *p << std::endl; // 5, value of x (p dereferenced) std::cout << "&p=" << &p << std::endl; // 0x7fff6447a820, address of p pointer // change value of x thru p // p = 6; // error, can't set int* int *p = 6; std::cout << "x=" << x << std::endl; // 6 int y = 2; // int *q = y; // error can't initiate type int, needs int* // pointer function int (*fp)(int); std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp std::cout << "fp=" << fp << std::endl; // 0, value of pointer fp fp = &next; // fp points function next(int) fp = next; std::cout << "&next=" << &next << std::endl; // 1, address of function? std::cout << "fp=" << fp << std::endl; // 1, value address of function? std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp? std::cout << "*fp=" << *fp << std::endl; // 1, address of function? // calling function thru pointer int = 0; = (*fp)(i); std::cout << "i=" << << std::endl; // 99 = fp(i); std::cout << "i=" << << std::endl; // 198 // function returns pointer function fptr fp_ptr = return_func_ptr(); std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl; // 1 std::cout << "fp_ptr=" << *fp_ptr << std::endl; // 1 std::cout << "&fp_ptr=" << *fp_ptr << std::endl; // 1 std::cout << "*fp_ptr=" << *fp_ptr << std::endl; // 1 int j = fp_ptr(1); std::cout << "j=" << j << std::endl; // 100 }
there pointer here seems not clear :
// pointer function int (*fp)(int); std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp std::cout << "fp=" << fp << std::endl; // 0, value of pointer fp
here fp
undefined. lines have undefined behaviour.
after :
// function returns pointer function fptr fp_ptr = return_func_ptr(); std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl; // 1 std::cout << "fp_ptr=" << *fp_ptr << std::endl; // 1 std::cout << "&fp_ptr=" << *fp_ptr << std::endl; // 1 // ^^^^^^^^^^ ^^^^^^^ std::cout << "*fp_ptr=" << *fp_ptr << std::endl; // 1
there 2 things here :
- on line pointed, i'm not sure wanted test.
also,
cout
doesn't have overload take function pointer, takebool
instead. should :std::cout << "fn_ptr=" << reinterpret_cast<void*>( fn_ptr ) << std::endl;
i suggest read article function pointer, explains need know : http://www.learncpp.com/cpp-tutorial/78-function-pointers/
Comments
Post a Comment