C/C++:常量数组的常量数组

发布于 2024-10-10 02:11:14 字数 60 浏览 0 评论 0原文

创建常量数组的常量数组的语法是什么?

我希望函数参数是常量 char* 字符串的常量数组。

What would be the syntax for creating a constant array of constant arrays?

I am wanting a function argument to be a constant array of constant char* strings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初见终念 2024-10-17 02:11:14

您可以通过将 const 放在第一个星号的右侧来完成此操作,例如

void f(const char *const *argument)

或等效地

void f(const char *const argument[])

,对于更多维度,只需添加更多 *const (我不会使用 [] 在这种情况下的替代方案):

void f(const char *const *const *argument) // 2D array of strings

You do this by putting const on the right of the first asterisk, e.g.

void f(const char *const *argument)

or equivalently

void f(const char *const argument[])

For more dimensions, simply add more *consts (I would not use the [] alternative in this case):

void f(const char *const *const *argument) // 2D array of strings
故事灯 2024-10-17 02:11:14

关键是向后写 C++(从右到左):

         char * const myVar[10] const;

...这表示 myVar 是一个指向 char 的 const 指针的 const 数组,长度为 10。

The key to this is to write the C++ backwards (right to left):

         char * const myVar[10] const;

...which says that myVar is an const array length 10 of const pointer to char.

萌能量女王 2024-10-17 02:11:14

我相信那将是

const char* const array[size][size] = { /* initializer */ }

一个即指向无法更改的字符的不可变指针数组的数组。

I believe that would be a

const char* const array[size][size] = { /* initializer */ }

That is, an array of arrays of immutable pointers to characters that can't be changed.

戏舞 2024-10-17 02:11:14

问题有点不清楚:您想要创建(定义)一个数组,还是将其传递给函数?

定义常量C-strings常量数组的语法是

const char array[2][14] = { "first string", "second string" };

要定义非字符串类型常量数组的常量数组,初始化程序有所不同:(

const int array[2][3] =
{
  { 1, 2, 3 },
  { 4, 5, 6 }
};

如果合适,你应该使数组static const。)

The question is a bit unclear: Do you want to create (define) an array, or pass it to a function?

The syntax to define a constant array of constant C-strings is

const char array[2][14] = { "first string", "second string" };

To define a constant array of constant arrays of non-string type, the initializer differs:

const int array[2][3] =
{
  { 1, 2, 3 },
  { 4, 5, 6 }
};

(If it's appropriate, you should make the array static const.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文