简体   繁体   English

如何创建静态分配的动态大小的数组?

[英]How can I create a statically allocated dynamically sized array?

typedef struct DictionaryEntry_s {
    char *key;
    char *value;
} DictionaryEntry;

typedef struct Dictionary_s {
    char *name;
    DictionaryEntry values[0];
} Dictionary;

//How can I do the following:
Dictionary myDictionary[] = { 
    {"synonyms",
        {"good", "cool"},
        {"bad", "evil"},
        {"awesome", "me"},
        {"like", "love"}, //etc....
        {0} //terminator
    },
    {"antonyms",
        {"good", "evil"},
        {"bad", "good"},
        {"awesome", "not me"}, ///...etc
        {0} //terminator
    },
    {0} //terminator
};

As you can see in the code, I want to create a statically allocated but dynamically sized array. 正如您在代码中看到的那样,我想创建一个静态分配但动态大小的数组。 I know how to loop through the data, its just that the compiler barfs at the declaration. 我知道如何遍历数据,只是编译器在声明中bar倒了。 While I am looking for a C solution, bonus points for C++ in addition. 当我在寻找C解决方案时,除了C ++外,还有积分。

Thanks! 谢谢!

C solution requires extra variables for defining internal arrays: C解决方案需要额外的变量来定义内部数组:

typedef struct DictionaryEntry_s {
    char *key;
    char *value;
} DictionaryEntry;

typedef struct Dictionary_s {
    char *name;
    DictionaryEntry* values;
} Dictionary;

//How can I do the following:
DictionaryEntry myDictionary0[] = {
        {"good", "cool"},
        {"bad", "evil"},
        {"awesome", "me"},
        {"like", "love"}, //etc....
        {0} //terminator
};
Dictionary myDictionary[] = { 
    {"synonyms", myDictionary0},
    // ...
    {0} //terminator
}; // <-- semicolon was missing here

C++ solution - requires std::vector<> - however it is not statically allocated, but dynamically, and it does not requires terminator: C ++解决方案-需要std::vector<> -但是它不是statically分配的,而是动态分配的,并且不需要终结符:

struct DictionaryEntry {
    char *key;
    char *value;
};

struct Dictionary {
    char *name;
    std::vector<DictionaryEntry> values;
};

//How can I do the following:
Dictionary myDictionary[] = { 
    {"synonyms",
      {
        {"good", "cool"},
        {"bad", "evil"},
        {"awesome", "me"},
        {"like", "love"}, //etc....
        {0} //terminator
      } 
    },
    //...
    {0} //terminator
}; // <-- semicolon was missing here   

In C++11 you can use initializer lists . 在C ++ 11中,可以使用初始化程序列表 You can define a DictionaryArray class with a constructor that takes one of these, and then write 您可以使用采用其中之一的构造函数定义DictionaryArray类,然后编写

DictionaryArray myArray({ /* some list */ });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建动态大小的链接列表数组? - How can I create a dynamically sized array of linked lists? 如果在动态/静态分配的双精度数组中输入字符,会发生什么情况? - What happens if I enter a character to a dynamically/statically allocated double array? 如何从任何迷宫 ASCII 文本文件中创建动态分配的二维数组? - How can I create a dynamically-allocated 2D array out of any maze ASCII text file? 如何使用一些参数初始化动态分配数组中的对象 - How can I initialize objects in a dynamically allocated array with some arguments 有没有办法在 C++ 中静态初始化动态分配的数组? - Is there a way to statically-initialize a dynamically-allocated array in C++? C ++动态分配静态大小的数组 - C++ dynamically allocated array of statically dimensioned arrays 正常声明数组时,memory 是静态分配、动态分配还是其他方式分配? - When an array is declared normally, is the memory allocated statically, dynamically or otherwise? 我可以创建一个只能动态分配的结构吗? - Can I create a struct that can only be dynamically allocated? 如何在 glTexImage2D 中使用动态大小的纹理数组? - How can I use a dynamically sized texture array with glTexImage2D? 我如何初始化动态分配的字符数组,所以我不会得到那些奇怪的字符? - how can i initialize a dynamically allocated array of characters so i don't get those weird characters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM