【发布时间】:2013-09-18 13:47:39
【问题描述】:
当我在结构中使用函数指针数组时,程序崩溃。
#include <stdio.h>
typedef void (*FUNCPTR)(void);
void Function_1()
{
printf(" In Function_1 \n");
}
void Function_2()
{
printf(" In Function_2 \n");
}
typedef struct St_FUNCPTR
{
FUNCPTR xxx[2];
}ST_FUNCPTR;
FUNCPTR fnptr1[2] =
{
Function_1,
Function_2
};
ST_FUNCPTR fnptr =
{
fnptr1
};
/* The intention is to call Function_1(); through array of function
pointers in the structure. */
int main()
{
// to call Function_1();
fnptr.xxx[0]();
return 0;
}
如果结构定义如下,则可以正常工作。
ST_FUNCPTR fnptr =
{
{Function_1,Function_2},
};
我的问题在哪里?
【问题讨论】:
-
我希望您的实际代码比这更复杂,因为
St_FUNCPTR结构似乎不必要地使事情复杂化,您可以直接使用fnptr1来做完全相同的事情。
标签: c structure function-pointers