在C中找出查找表的大小

在C中找出查找表的大小,c,arrays,struct,C,Arrays,Struct,是否有方法确定以下查找表中消息元素的数量,或者我是否需要在结构中显式设置int size typedef struct { int enable; char* message[3]; } lookuptable; lookuptable table[] = { {1, {"Foo", "Bar", "Baz"}}, // # 3 {1, {"Foo", "Bar"}}, // # 2 {1, {"Foo"}},

是否有方法确定以下查找表中
消息
元素的数量,或者我是否需要在结构中显式设置
int size

typedef struct {
    int enable;
    char* message[3];
} lookuptable;

lookuptable table[] = {
    {1, {"Foo", "Bar", "Baz"}}, // # 3
    {1, {"Foo", "Bar"}},        // # 2
    {1, {"Foo"}},               // # 1
    {1, {"Foo", "Baz"}},        // # 2
};

不,没有办法。您必须将元素数存储在某个位置,或者用幻数或空值终止数组。

不,没有办法做到这一点。您必须将元素数存储在某个位置,或使用幻数或NULL终止数组。

消息数组中始终只有3个消息元素,因为您已将其定义为大小为3。您未初始化的数组元素将被初始化为NULL,因此您可以通过以下方式循环初始化(非NULL)元素:

lookuptable *table_entry = ...
for (int i = 0; i < 3 && table_entry->message[i]; i++) {
    ...do something...

消息数组中始终只有3个消息元素,因为您已将其定义为大小为3。您未初始化的数组元素将被初始化为NULL,因此您可以通过以下方式循环初始化(非NULL)元素:

lookuptable *table_entry = ...
for (int i = 0; i < 3 && table_entry->message[i]; i++) {
    ...do something...

很高兴知道。谢谢你的信息!很高兴知道。谢谢你的信息!