argv 中的字符串是否可以修改? [英] Are the strings in argv modifiable?

查看:21
本文介绍了argv 中的字符串是否可以修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚写了一个小程序,用 C 读取命令行参数,没什么难度.我也在修改它们,例如将参数的第一个字符更改为大写.

I just wrote a small program that reads command line arguments in C, nothing too difficult. I was also modifying them, for example changing the first character of the parameter to uppercase.

我知道您不应该修改字符串文字,因为它会导致未定义的行为,所以我只是想知道 *argv[] 中的字符串是否是您不应该更改的文字.

I know that you shouldn't modify string literals as it can cause undefined behavior, so was just wondering if the strings in the *argv[] are literals that you shouldn't change.

int main(int argc, char *argv[])

推荐答案

来自 C11 标准草案 N1570,§5.1.2.2.1/2:

From the C11 standard draft N1570, §5.1.2.2.1/2:

参数argcargv以及argv指向的字符串数组应可由程序修改,并保留其最后存储的程序启动和程序终止之间的值.

The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

它们是可修改的.这意味着它们不是字符串文字.

They are modifiable. That means they are not string literals.

但是要小心:上面的引用仅指指向字符串的指针,不包括argv[argc]1.
来自 C11 标准草案 N1570,§5.1.2.2.1/2(同上)1:

But be careful: the upper citation only refers to pointers to strings, excluding the obligatory null pointer at argv[argc]1.
From the C11 standard draft N1570, §5.1.2.2.1/2 (same as above)1:

argv[argc] 应为空指针

<小时>

注意事项:


Notes:

  • 关于这句话的一些事情:

  • Something regarding this sentence:

我知道你不应该修改字符串文字,因为它会导致未定义的行为 [...]

I know that you shouldn't modify string literals as it can cause undefined behavior [...]

可以"吗?它总是如此.未定义的行为包括预期的(好像定义良好的)和意外的行为.

"can"? It does always. Undefined behavior includes expected, as if well-defined, and unexpected behavior.

1 感谢@black!

1 Thanks to @black!

这篇关于argv 中的字符串是否可以修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆