gpt4 book ai didi

c - 全局使用 argv 指针安全吗?

转载 作者:太空狗 更新时间:2023-10-29 16:22:57 26 4
gpt4 key购买 nike

全局使用argv指针安全吗?还是有可能失效的情况?

即:这段代码安全吗?

char **largs;
void function_1()
{
printf("Argument 1: %s\r\n",largs[1]);
}
int main(int argc,char **argv)
{
largs = argv;
function_1();
return 1;
}

最佳答案

是的,全局使用argv是安全的;您可以像在程序中使用任何 char** 一样使用它。 C99 标准甚至规定了这一点:

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.

C++ 标准没有类似的段落,但同样是隐含的,没有相反的规则。

请注意,C++ 和 C 是不同的语言,您应该只选择一种来提出您的问题。

关于c - 全局使用 argv 指针安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31605132/

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