gpt4 book ai didi

c - 从c中的函数返回字符串

转载 作者:太空狗 更新时间:2023-10-29 16:08:38 25 4
gpt4 key购买 nike

我对我们如何能够从函数返回字符串感到困惑。

char* someFunction()
{
return "Hello, World"
}

上面的 return 语句不应该抛出“函数返回局部变量的地址”,它与函数有何不同:

char* newFunction()
{
char temp[] = "Hello, World";
return temp;
}

实际上确实给出了上述警告。

最佳答案

在第一种情况下,字符串始终位于固定地址的常量只读内存中。在第二种情况下,字符串被本地放在堆栈上,因此是临时的。

关于c - 从c中的函数返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11088220/

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