c++我怎样才能正确预定义 char* 数组?
我就是这样做的:
int argc = 9;
char* argv[argc];
argv[0] = "c:/prog.exe";
但我注意到它已被弃用。什么是更好的方法?
I am doing it that way:
int argc = 9;
char* argv[argc];
argv[0] = "c:/prog.exe";
but I get notice, that it is deprecated. What is better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须将其设置为 const:
... 或不使用常量字符串文字:
You have to either make it const:
... or not use the constant string literals:
除了使用常量表达式以外的东西来表示数组大小的问题之外......
已被弃用的是将字符串文字静默转换为
char*
。这曾经是好的:现在必须是:
这个弃用实际上位于 C++03 的附录中。
Besides the problem of using something other than a constant expression for your array size...
The thing that has been deprecated is the silent casting of string literals to
char*
. This used to be OK:Now it has to be:
This deprecation is actually in an Appendix in C++03.
让我们分析一下您在这里所做的事情:
我的猜测是您并没有尝试执行我上面描述的操作。尝试这样的事情:
-或-
Let analyze what you are doing here:
My guess is that you are not trying to do what I've described above. Try something like this:
-or -
尝试使用
const
来指示字符串不会被修改。这里,argc 也会在编译时自动计算,因此出错的可能性较小(感谢 Goz 的建议)。
Try using
const
to indicate that the strings won't be modified.Here,
argc
will also be calculated at compile time automatically so there's a lesser chance of error (thanks to Goz for the suggestion).弗拉德+1。
我对这里发生的情况进行了更多解释:
您收到“已弃用”警告,因为这样的代码:
现在具有类型
const char*
,而不是char*
。并且字符串文字可以转换为char*
,以便在const
不那么严格时保留与旧约定的一些兼容性。但是,不建议将字符串文字从const char*
转换为char*
,您不应依赖它。为什么?字符串文字是指向常量内存的指针,这就是为什么它需要是 const char* 的原因。
+1 for Vlad.
Some more explanation from me on what happens here:
You get the "deprecated" warning, because such code:
now has type
const char*
, notchar*
. And string literals can be converted tochar*
, to retain some compatibility with the older conventions whenconst
wasn't that strict. But conversion of a string literal tochar*
fromconst char*
is deprecated and you should not rely on it.Why? String literal is a pointer to constant memory, that's why it needs to be
const char*
.除了其他人指出的关于 const 字符串文字被分配给非常量 char 指针以及在 main() 的参数列表之外声明 argv 和 argc 的奇怪之处之外,这里的这一行还有一个额外的问题
: >char* argv[argc];
在 C++ 中只能使用整型常量表达式来表示数组大小;整数常量表达式是程序源中的文字整数(例如“5”或“10”),枚举值(例如“enum Colors {red, green, blue};”中的“red”),sizeof表达式,或用 const 声明的 int 变量:
许多 C++ 编译器实现 C99 风格的可变长度数组,因此在使用它们时可能不会收到任何抱怨,但如果您想编写可移植代码,最好还是避免使用它们。
Other than what everyone else has pointed out about const string literals being assigned to non-const char pointers and the weirdness of declaring argv and argc outside of main()'s parameter list, there is an additional problem with this line here:
char* argv[argc];
You can only use integer constant expressions for array sizes in C++; an integer constant expression being a literal integer in the source of your program (like "5" or "10"), an enumerations value (like "red" from "enum colors {red, green, blue};"), a sizeof expression, or an int variable declared with const:
Many C++ compilers implement C99-style variable-length arrays, so you may not get any complaint when you use them, but they are still best avoided if you want to write portable code.