c++我怎样才能正确预定义 char* 数组?

发布于 2024-10-07 18:34:14 字数 128 浏览 4 评论 0原文

我就是这样做的:

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 技术交流群。

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

发布评论

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

评论(6

美煞众生 2024-10-14 18:34:14

您必须将其设置为 const:

const char *argv[] = { "Arg1", "Arg2", "..." };

... 或不使用常量字符串文字:

int argc = 9;
char* argv[argc];
char prog_name[] = "c:/prog.exe";
argv[0] = prog_name;

You have to either make it const:

const char *argv[] = { "Arg1", "Arg2", "..." };

... or not use the constant string literals:

int argc = 9;
char* argv[argc];
char prog_name[] = "c:/prog.exe";
argv[0] = prog_name;
夜司空 2024-10-14 18:34:14

除了使用常量表达式以外的东西来表示数组大小的问题之外......

已被弃用的是将字符串文字静默转换为 char*。这曾经是好的:

char * hello = "hello";

现在必须是:

char const* hello = "hello";

这个弃用实际上位于 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:

char * hello = "hello";

Now it has to be:

char const* hello = "hello";

This deprecation is actually in an Appendix in C++03.

神爱温柔 2024-10-14 18:34:14

让我们分析一下您在这里所做的事情:

// Create an int with value 9.
int argc = 9;

// Create an array of char* pointers of size 9
char* argv[argc];

// Assign the the first pointer to the global data string "C:\prog.exe"
argv[0] = "c:/prog.exe";

我的猜测是您并没有尝试执行我上面描述的操作。尝试这样的事情:

// create an array of characters
char argv[] = "C:/prog.exe";

// argc in now the length of the string
int argc = sizeof argv;

-或-

// create an array of strings
char* argv[] = {"C:/prog.exe"};
// argc is now the number of strings in the array
int argc = 1;

Let analyze what you are doing here:

// Create an int with value 9.
int argc = 9;

// Create an array of char* pointers of size 9
char* argv[argc];

// Assign the the first pointer to the global data string "C:\prog.exe"
argv[0] = "c:/prog.exe";

My guess is that you are not trying to do what I've described above. Try something like this:

// create an array of characters
char argv[] = "C:/prog.exe";

// argc in now the length of the string
int argc = sizeof argv;

-or -

// create an array of strings
char* argv[] = {"C:/prog.exe"};
// argc is now the number of strings in the array
int argc = 1;
☆獨立☆ 2024-10-14 18:34:14

尝试使用 const 来指示字符串不会被修改。

const char* argv[] = { "c:/prog.exe" };
const int argc = sizeof(argv) / sizeof(argv[0]);

int main()
{
    for(int i = 0; i < argc; ++i)
    {
        ::printf("%s\n", argv[i]);
    }
}

这里,argc 也会在编译时自动计算,因此出错的可能性较小(感谢 Goz 的建议)。

Try using const to indicate that the strings won't be modified.

const char* argv[] = { "c:/prog.exe" };
const int argc = sizeof(argv) / sizeof(argv[0]);

int main()
{
    for(int i = 0; i < argc; ++i)
    {
        ::printf("%s\n", argv[i]);
    }
}

Here, argc will also be calculated at compile time automatically so there's a lesser chance of error (thanks to Goz for the suggestion).

∞琼窗梦回ˉ 2024-10-14 18:34:14

弗拉德+1。

我对这里发生的情况进行了更多解释:

您收到“已弃用”警告,因为这样的代码:

"asdf"

现在具有类型 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:

"asdf"

now has type const char*, not char*. And string literals can be converted to char*, to retain some compatibility with the older conventions when const wasn't that strict. But conversion of a string literal to char* from const 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*.

傲性难收 2024-10-14 18:34:14

除了其他人指出的关于 const 字符串文字被分配给非常量 char 指针以及在 main() 的参数列表之外声明 argv 和 argc 的奇怪之处之外,这里的这一行还有一个额外的问题

: >char* argv[argc];

在 C++ 中只能使用整型常量表达式来表示数组大小;整数常量表达式是程序源中的文字整数(例如“5”或“10”),枚举值(例如“enum Colors {red, green, blue};”中的“red”),sizeof表达式,或用 const 声明的 int 变量:

// can hold 30 ints
int myarray1[30];

// can hold as many ints as myarray1 is wide in bytes
int myarray2[sizeof(myarray1)];

// C++ does not support variable-length arrays like C99 does, so if an int
// variable is used to specify array size, it must be marked const:
const int myarray3_size = 42;
int myarray3[myarray3];

许多 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:

// can hold 30 ints
int myarray1[30];

// can hold as many ints as myarray1 is wide in bytes
int myarray2[sizeof(myarray1)];

// C++ does not support variable-length arrays like C99 does, so if an int
// variable is used to specify array size, it must be marked const:
const int myarray3_size = 42;
int myarray3[myarray3];

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.

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