如何声明和初始化字符串的二维数组? [英] How to declare and init two dimensional array of strings?

查看:185
本文介绍了如何声明和初始化字符串的二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这样的东西

const char **nodeNames[] = 
{
    {"RootNode", "Reference", "Node_1", "Node_2", "Node_3"},
    {"RootNode", "Hips", "Node_1", "Node_2", "Node_3"},
    {"RootNode", "Heviest", "Node_1", "Node_2", "Node_3"},
};

但使用先前的声明,我遇到了错误。

but with previous declaration, i got an error.

我如何在代码中引用它?

And how can i reference to it in code?

推荐答案

看起来像您想要一个<$的二维数组c $ c> const char * :

const char *nodeNames[][5] =
{                 // ^^ this dimension can be deduced by the compiler, the rest not
    {"RootNode", "Reference", "Node_1", "Node_2", "Node_3"},
    {"RootNode", "Hips", "Node_1", "Node_2", "Node_3"},
    {"RootNode", "Heviest", "Node_1", "Node_2", "Node_3"}
};

请注意,您需要明确指定除主要尺寸之外的所有尺寸。

Note that you need to explicitly specify all but the major dimension's size.

这并不完全像3D字符数组,因为您的字符串大小不尽相同。我相信您知道这一点,并且不会例如取消引用 nodeNames [0] [2] [7] ,该引用将超出<$ c的结尾$ c>节点_1 。

This doesn't behave exactly like a 3D array of chars because your strings are not all of the same size. I trust you're aware of that and you won't for example dereference nodeNames[0][2][7], which would go beyond the end of "Node_1".

这篇关于如何声明和初始化字符串的二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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