在涉及自己创建的函数指针,为了方便调试,希望将函数指针当前指向的函数名打印出来。直接打印不可能,所以需要在定义指针时添加上指示符。
例如stackoverflow上的一段代码样例
http://stackoverflow.com/questions/351134/how-to-get-functions-name-from-functions-pointer-in-c// Define it like this
typedef struct
{
char *dec_text;
#ifdef _DEBUG_FUNC
void (*action)(char);
#endif
} func_Struct;
// Initialize it like this
func_Struct func[3]= {
#ifdef _DEBUG_FUNC
{"my_Set(char input)",&my_Set}};
{"my_Get(char input)",&my_Get}};
{"my_Clr(char input)",&my_Clr}};
#else
{&my_Set}};
{&my_Get}};
{&my_Clr}};
#endif
// And finally you can use it like this
func[0].action( 0x45 );
#ifdef _DEBUG_FUNC
printf("%s",func.dec_text);#endiflinux下可以通过backtrace、backtrace_symbols、backtrace_symbols_fd这样的函数,结合-rdynamic
本文详细介绍了在C语言中使用函数指针获取当前指向函数的名称,并提供了一种方法来实现调试功能。通过定义特定的结构体和在结构体中包含函数指针和函数名字符串,可以方便地在代码中调用和打印函数名。此外,文章还提及了在Linux环境下使用backtrace等函数进行更深入的调试。
1万+

被折叠的 条评论
为什么被折叠?



