Ich bin mir nicht sicher, ob du das willst, aber du könntest so etwas machen. Deklarieren eine Struktur, einen Funktionsnamen und die Adresse zu halten, und eine Reihe von Funktionen im Dateibereich:
#define FNUM 3
struct fnc {
void *addr;
char name[32];
};
void (*f[FNUM])();
struct fnc fnames[FNUM];
Initialisiere diese im Code manuell von Funktionsnamen, z.B.
Eine Funktion zum Durchsuchen des Arrays, z.
char *getfname(void *p)
{
for (int i = 0; i < FNUM; i++) {
if (fnames[i].addr == p)
return fnames[i].name;
}
return NULL;
}
Ich habe einen schnellen Test davon durchgeführt. Ich initialisierte das Array in main
und rief foo1()
. Hier ist meine Funktion, und die Ausgabe:
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'
Oder allgemeiner:
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++ Version @ http://stackoverflow.com/q/679021/139010 –
Es gibt keine Standard-Weg, dies zu tun . Welchen Compiler benutzen Sie? –
Wie man den Namen der Funktion vom Funktionszeiger in C bekommt http://stackoverflow.com/questions/351134/how-to-get-functions-name-from-functions-pointer-in-c – zzk