visual c++ - C++ display address of a function in a MessageBox -


i trying display memory address of function in messagebox, doesn't display want.

i want pass function address of callback function function, tried address.

i looked @ this example , tried show in messagebox first instead printing console, before using it.

how tried it:

char ** fun() {     static char * z = (char*)"merry christmas :)";     return &z; } int main() {     char ** ptr = null;      char ** (*fun_ptr)(); //declaration of pointer function     fun_ptr = &fun;      ptr = fun();      char c[256];      snprintf(c, sizeof(c), "\n %s \n address of function: [%p]", *ptr, fun_ptr);     messageboxa(nullptr, c, "hello world!", mb_iconinformation);      snprintf(c, sizeof(c), "\n address of first variable created in fun() = [%p]", (void*)ptr);     messageboxa(nullptr, c, "hello world!", mb_iconinformation);      return 0; } 

but, these messageboxes display large numbers , seems null.

i display them in messagebox in linked post's example outputs.

thanks in advance.

i made changes in code make more c++-y, , seems work:

  1. i'm using std::cout print instead of snprintf.
  2. i'm converting pointer address std::string via std::stringstream. should work without problem messagebox.
  3. i changed function signature const char** avoid problems.

final code:

#include <iostream> #include <sstream>  const char** fun() {     static const char* z = "merry christmas :)";     return &z; } int main() {     const char** (*fun_ptr)() = fun;      const char** ptr = fun();      std::cout << "address of function: [" << (void*)fun_ptr  << "]" << std::endl;     std::cout << "address of first variable created in fun() = [" << (void*)ptr  << "]" << std::endl;      std::stringstream ss;     ss << (void*)fun_ptr;     std::cout << "address std::string = [" << ss.str() << "]" << std::endl;      return 0; } 

outputs:

address of function: [0x106621520] address of first variable created in fun() = [0x1066261b0] address std::string = [0x106621520] 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -