나는 이것이 당신이 원하는 것이라고 확신하지 못하지만, 이렇게 할 수 있습니다.
#define FNUM 3
struct fnc {
void *addr;
char name[32];
};
void (*f[FNUM])();
struct fnc fnames[FNUM];
수동 함수 이름, 예컨대하여 코드에서 이러한 초기화한다 : 함수 이름, 주소 및 파일 범위 함수의 어레이를 유지하도록하는 구조를 선언
fnames[0] = (struct fnc){foo1, "foo1"}; // function address + its name
fnames[1] = (struct fnc){foo2, "foo2"};
fnames[2] = (struct fnc){foo3, "foo3"};
배열을 검색하는 함수를 만듭니다.
char *getfname(void *p)
{
for (int i = 0; i < FNUM; i++) {
if (fnames[i].addr == p)
return fnames[i].name;
}
return NULL;
}
나는 이것을 신속하게 테스트했다. main
에 배열을 초기화하고 foo1()
을 호출했습니다. 여기 내 기능, 그리고 출력 :
void foo1(void)
{
printf("The pointer of the current function is %p\n", getfnp(__func__));
printf("The name of this function is %s\n", getfname(getfnp(__func__)));
printf("The name of the function at pointer f[2] (%p) is '%s'\n", f[2],
getfname(f[2]));
}
The pointer of the current function is 0x400715
The name of this function is foo1
The name of the function at pointer f[2] (0x40078c) is 'foo3'
또는 더 일반적으로 :
이 일을의 표준 방법은 없습니다
void foo2(void)
{
for (int i = 0; i < FNUM; i++) {
printf("Function f[%d] is called '%s'\n", i, getfname(f[i]));
}
}
Function f[0] is called 'foo1'
Function f[1] is called 'foo2'
Function f[2] is called 'foo3'
C++ 버전 @ http://stackoverflow.com/q/679021/139010 –
. 어떤 컴파일러를 사용하고 있습니까? –
C의 함수 포인터에서 함수의 이름을 얻는 방법 http://stackoverflow.com/questions/351134/how-to-get-functions-name-from-functions-pointer-in-c – zzk