C语言 C (Arduino) 中的字符串数组(字符数组)。我该如何实现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12930978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:07:38  来源:igfitidea点击:

Array of strings (char array) in C (Arduino). How do I accomplish it?

carraysarduinochars

提问by z3cko

I want to access certain data which basically looks like this:

我想访问某些基本如下所示的数据:

char* a[]={
    "0000000000",
    "0000000000",
    "0011111100",
    "0000100100",
    "0000100100",
    "0011111100",
    "0000000000",
    "0000000000",
};

I have around 200 of those data sets and want to access it in the way.

我有大约 200 个这样的数据集,并希望以这种方式访问​​它。

fooBar[23];--> This should return the 23rd character array (which looks like the example listed above).

fooBar[23];--> 这应该返回第 23 个字符数组(看起来像上面列出的例子)。

As far as I understand from my other programming knowledge, I would need an array of Strings. The array index is my lookup number (which will be a maximum of 255). The array values are the character arrays as shown above.

据我了解其他编程知识,我需要一个字符串数组。数组索引是我的查找编号(最多为 255)。数组值是如上所示的字符数组。

How can this be accomplished with C (Arduino IDE)?

如何使用 C (Arduino IDE) 实现这一点?

回答by taufique

Just use a two dimensional array. Like:

只需使用二维数组。喜欢:

char a[][]={
    "0000000000",
    "0000000000",
    "0011111100",
    "0000100100",
    "0000100100",
    "0011111100",
    "0000000000",
    "0000000000",
};

回答by hmjd

Based on your comment, I thinkthis is what you are asking for:

根据您的评论,我认为这就是您的要求:

const char* data_sets[][200] =
    {
        { "00000", "11111",         },
        { "22222", "33333", "44444" },
        { "55555"                   },
    };

Each entry in data_setsis an array of 200 const char*. For accessing:

中的每个条目data_sets都是一个 200 的数组const char*。访问:

for (size_t i = 0; i < sizeof(data_sets) / sizeof(data_sets[0]); i++)
{
    const char** data_set = data_sets[i];
    printf("data_set[%u]\n", i);
    for (size_t j = 0; data_set[j]; j++)
    {
        printf("  [%s]\n", data_set[j]);
    }
}

See online demo at http://ideone.com/6kq2M.

请参阅http://ideone.com/6kq2M 上的在线演示。