由於奧菲爾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