<pre name="code" class="cpp">typedef void (*app_static_init)(void **static_params, int cnt); typedef void (*app_dynamic_init)(void *usr, void *dynamic_params); typedef void *(*app_create)(void *static_params, void *dynamic_params); typedef pthread_t (*app_start)(void *handle, int priority); typedef void (*app_delete)(void *handle); typedef struct app_task_spawn_info_t { int task_type; char task_name[64]; int task_priority; void *static_params; void *dynamic_params; void *app_handle; app_static_init static_init_app; app_dynamic_init dynamic_init_app; app_create create_app; app_start start_app; app_delete delete_app; }app_task_spawn_info_t;
static app_task_spawn_info_t app_list[]= { {APP_TYPE_OUTBOUND_TRANS_ENC, "outbound_trans_pthread", 90, NULL,\ &glb_outBound_trans_dynamic_params_default, gCtrol.handle_outBound_trans,\ outBound_trans_static_init, NULL, outBound_trans_create, outBound_trans_start, outBound_trans_delete}, };
xx.c:56:5: error: initializer element is not constant
xx.c:56:5: error: (near initialization for 'app_list[0].app_handle')
编译时显示的错误,直接翻译"初始化的元素不是常量"。
问题出在结构体数组元素
gCtrol.handle_outBound_trans
结构体初始化过程可以写成如下:
app_list[0].app_handle = gCtrol.handle_outBound_trans;
注意app_list是一个静态全局变量,初始化的时候其中值app_list[0].app_handle应该是一个常量,而gCtrol.handle_outBound_trans本身是变量,其值是不确定的,所以编译器在编译的时候即不能将其放入.data段,又不能放入.bss段,所以导致编译错误。
那么怎样来修改这个错误呢,我想可以使用双指针来解决,如下所示:
typedef struct app_task_spawn_info_t { int task_type; char task_name[64]; int task_priority; void *static_params; void *dynamic_params; void **app_handle; app_static_init static_init_app; app_dynamic_init dynamic_init_app; app_create create_app; app_start start_app; app_delete delete_app; }app_task_spawn_info_t;
static app_task_spawn_info_t app_list[]= { {APP_TYPE_OUTBOUND_TRANS_ENC, "outbound_trans_pthread", 90, NULL,\ &glb_outBound_trans_dynamic_params_default, &gCtrol.handle_outBound_trans,\ outBound_trans_static_init, NULL, outBound_trans_create, outBound_trans_start, outBound_trans_delete}, };
这样展开后:
app_list[0].app_handle = &gCtrol.handle_outBound_trans;
变量gCtrol.handle_outBound_trans的地址是唯一确定的,这样app_list[0].app_handle初始化的值就是个常量。
问题解决。