看gdb源码的时候无意发现一个奇怪的写法, 仔细查看了之后明白了其中的秘密
fun()->mem1 = xx;
fun()->mem2(4);
这是我实现的一个代码:
#include <stdio.h>
#include <stdlib.h>
typedef int (*print_fun_t)(const char *, ...);
struct inferior
{
int attach_flag;
print_fun_t print;
};
/* The Current Inferior. */
static struct inferior *current_inferior_ = NULL;
struct inferior*
current_inferior (void)
{
return current_inferior_;
}
void
set_current_inferior (struct inferior *inf)
{
current_inferior_ = inf;
}
int main()
{
struct inferior *inf;
inf = (struct inferior *)malloc(sizeof(struct inferior));
inf->print = printf;
set_current_inferior(inf);
current_inferior()->attach_flag = 10;
current_inferior()->print("my attach_flag: %d\n", current_inferior()->attach_flag);
return 0;
}