经过多年的java,javascript,python,我不仅忘记了C ++,而且还混淆了这种语法。
http://heavycoder.com/tutorials/lua_embed.php
static const luaL_reg lualibs[] =
{
{ "base", luaopen_base },
{ NULL, NULL }
};
使用2D数组的lualibs init? luaL_reg是一个类型,但显然不是数组,
const luaL_reg *lib;
for (lib = lualibs; lib->func != NULL; lib++)
{
lib->func(l);
lua_settop(l, 0);
}
答案 0 :(得分:2)
luaL_reg
是一个包含2个元素的struct
,this就是Google快速搜索的内容。
第一个代码段是创建luaL_reg
struct
s:
struct
初始化为两个值:{ "base", luaopen_base }
luaL_reg
struct
设置为:{ NULL, NULL }
底线,它是不一个2D数组,而是一个structs
数组,其中每个struct
包含两个元素。
第二个例子现在应该是相当不言自明的; lib
是指向luaL_reg
struct
的指针。
答案 1 :(得分:2)
luaL_reg
看起来可能如下所示。
typedef struct luaL_reg_t {
char const * const name;
void(*func)(< type_of_<l> >);
} luaL_reg;
可以使用{}
设置对象的成员,如下例所示,这会将成员name
设置为指向"hello world"
和func
的位置获得my_function
的地址。
luaL_reg obj = {"hello world", my_function};
在初始化数组成员时,也可以使用上一个代码段中显示的语法。在下面的代码片段中,const luaL_reg实例数组设置为包含两个对象,第一个对象的name
= "base"
和func
设置为luaopen_base
。
说清楚;以下是不 2D数组,但使用const luaL_reg
初始化的{}
数组来设置每个实例的成员。
static const luaL_reg lualibs[] =
{
{ "base", luaopen_base },
{ NULL, NULL }
};
最后一个元素用于简化迭代我们的数组,将两个成员设置为NULL
可以很容易地看到我们何时到达最后一个元素。
即将发布的代码片段中的循环利用了这一点。只要成员func
不等于NULL
,我们就没有到达数组的末尾。
for (lib = lualibs; lib->func != NULL; lib++) {
lib->func(l);
lua_settop(l, 0);
}