在C数组理想的数据类型 [英] Ideal data type for an array in C

查看:119
本文介绍了在C数组理想的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储的关键和可变大小的数组之间的静态连接。

I'd like to store a static association between a key and a variable size array.

"foo" => ["bar", "awe"]  
"foo2" => ["bar2", "awe2", "gruh"]

这阵绝不会在程序执行过程中发生改变,它只是一种静态的字符串变量数组与字符串键关联。

This array will never change during the program execution, it is just a way to statically associate a variable array of string with a string key.

子阵列的数量是非常低的(小于100)。

Number of sub-arrays is very low (< 100).

我怎么能做到这一点在纯 C

How can I do that in pure C?

推荐答案

好吧,这里是一个解决方案,有人给了我:

Ok, here is a solution someone gave me:

struct
{
  const char *key;
  const char **values;
} key_to_values[] =
{
  { .key = "foo", .values = (const char *[]) { "val1", "val2", NULL } },
  { .key = "bar", .values = (const char *[]) { "val3", "val4", "val5", NULL } },
};

这篇关于在C数组理想的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆