由于奥菲尔says in his answer,你基本上无法做到这一点。这些函数的名称只在链接完成之前才存在(它们可能会在调试数据之后忍受)。链接后它只是指针。
如果你愿意的地方存放函数地址,并比较它们,你可以做有点像这样:
$猫3922500.c
#include <stdio.h>
char *fxname(void *fx) {
if (fx == fprintf) return "fprintf";
if (fx == gets) return "gets";
if (fx == scanf) return "scanf";
return "(unknown)";
}
int main(void) {
void (*fx)(void);
fx = gets; printf("name: %s\n", fxname(fx));
fx = putchar; printf("name: %s\n", fxname(fx));
return 0;
}
$ GCC 3922500.c
3922500.c: In function 'main':
3922500.c:12: warning: assignment from incompatible pointer type
3922500.c:13: warning: assignment from incompatible pointer type
/tmp/ccvg8QvD.o: In function `fxname':
3922500.c:(.text+0x1d): warning: the `gets' function is dangerous and should not be used.
$ ./a.out
name: gets
name: (unknown)
来源
2010-10-13 10:28:55
pmg
可能的重复[如何从C函数的指针获取函数的名称](http://stackoverflow.com/questions/351134/how-to-get-functions-name-from-functions-pointer-in-c) – Mundi 2012-08-25 07:33:04