C++ 如何创建 const char* 的静态 const 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10998343/
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
How to create a static const array of const char*
提问by Tommy
I tried the following line:
我尝试了以下行:
static const const char* values[];
But I get the following warning on VC++ warning C4114:
但是我在 VC++ 警告 C4114 上收到以下警告:
same type qualifier used more than once.
多次使用相同类型的限定符。
What is the correct declaration? The goal is to create an immutable array of c strings.
什么是正确的声明?目标是创建一个不可变的 c 字符串数组。
回答by Mesop
You wrote const const
instead of static const char* const values[];
(where you define the pointer and the underlying values as const
)
您编写了const const
而不是static const char* const values[];
(您将指针和基础值定义为const
)
Also, you need to initialize it:
此外,您需要对其进行初始化:
static const char* const values[] = {"string one", "string two"};
static const char* const values[] = {"string one", "string two"};
回答by Attila
Try
尝试
static const char* const values[];
The idea is to put the two const
s on either side of *
: the left belongs to char
(constant character), the right belongs to char*
(constant pointer-to-character)
这个想法是把两个const
s 放在两边*
:左边属于char
(常量字符),右边属于char*
(常量指针到字符)