gpt4 book ai didi

c - 在 C 命令行参数中为 argv 分配的缓冲区大小

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:23 25 4
gpt4 key购买 nike

当我们编写可以接受命令行参数的 C 程序时,我们将 main 函数编写为:

int main(int argc, char **argv) {
. . .
}

我们知道 argc 大于等于 1所以,至少 argv[0] 存在。我的问题是为 argv[0] 和后续参数分配的缓冲区大小是多少,如果有的话,例如argv[1], argv[2] 等等?

最佳答案

We know argc is greater than equal to 1

嗯,那不是真的。它应该是非负的。也可以为 0。

如果 argv[n] 存在,argv[n] 的大小就是您平台中指针的大小。指针指向的内存大小与提供的命令行参数字符串(由底层环境提供)相同。

引用 C11,章节 §5.1.2.2.1

  • The value of argc shall be nonnegative.

和,

  • If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. [...]

  • If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

关于c - 在 C 命令行参数中为 argv 分配的缓冲区大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50619964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com