C/C++:常量数组的常量数组
创建常量数组的常量数组的语法是什么?
我希望函数参数是常量 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过将
const
放在第一个星号的右侧来完成此操作,例如或等效地
,对于更多维度,只需添加更多
*const
(我不会使用[]
在这种情况下的替代方案):You do this by putting
const
on the right of the first asterisk, e.g.or equivalently
For more dimensions, simply add more
*const
s (I would not use the[]
alternative in this case):关键是向后写 C++(从右到左):
...这表示 myVar 是一个指向 char 的 const 指针的 const 数组,长度为 10。
The key to this is to write the C++ backwards (right to left):
...which says that myVar is an const array length 10 of const pointer to char.
我相信那将是
一个即指向无法更改的字符的不可变指针数组的数组。
I believe that would be a
That is, an array of arrays of immutable pointers to characters that can't be changed.
问题有点不清楚:您想要创建(定义)一个数组,还是将其传递给函数?
定义常量C-strings常量数组的语法是
要定义非字符串类型常量数组的常量数组,初始化程序有所不同:(
如果合适,你应该使数组
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
To define a constant array of constant arrays of non-string type, the initializer differs:
(If it's appropriate, you should make the array
static const
.)