我在下面列出了一些示例代码,问题是如果有一种方法让函数名从struct_name访问数值的值?函数指针:访问结构中的数据?
typedef struct struct_name {
int number
void (*func)();
} * struct_name_ptr;
void function_name() {
//access number from struct
}
main() {
struct_name_ptr newobject;
newobject->func=&function_name;
newobject->func(); //can it print the value of the number in the structure above?
}
除非你改变'func'的签名就可以接受参数是不可能的。 – UmNyobe
看到这个问题[如何在C中模拟OO风格的多态?](http://stackoverflow.com/questions/524033/how-can-i-simulate-oo-style-polymorphism-in-c) –
这不能工作;你已经将'struct_name_ptr'声明为一个指针,'newobject'指向未定义的内存,一个解引用是未定义的行为 – user411313