How to see which function is assigned to a function pointer at runtime ..?

#include<stdio.h> int add(int i,int j) { printf("\n%s\n",__FUNCTION__); return (i*j); } int (*fp)(int,int); void main() { int j=2; int i=5; printf("\n%s\n",__FUNCTION__); fp=add; printf("\n%d\n",(*fp)(2,5)); printf("\n%s\n",*fp); } 
+4
source share
1 answer

You can compare a pointer to a function with a pointer to a function. Like this:

  if (fp==add) printf("\nadd\n"); 

There are no other (standard) methods 1 .

it

 printf("\n%s\n",*fp); 

- compilation error.


There are certain platforms. For linux, this works:

 #include<stdio.h> #include <execinfo.h> int add(int i,int j) { printf("\n%s\n",__FUNCTION__); return (i*j); } int (*fp)(int,int); union { int (*fp)(int,int); void* fp1; } fpt; int main() { fp=add; fpt.fp=fp; char ** funName = backtrace_symbols(&fpt.fp1, 1); printf("%s\n",*funName); } 
+4
source

Source: https://habr.com/ru/post/1502727/


All Articles