gpt4 book ai didi

c - 返回指向 C 中临时对象的指针

转载 作者:太空狗 更新时间:2023-10-29 15:50:58 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
returning a pointer to a literal (or constant) character array (string)?

下面的代码是否正确?

const char* state2Str(enum State state)
{
switch (state)
{
case stateStopped: return "START";
case stateRunning: return "RUNNING";
default: return "UNKNOWN";
}
}

printf("State is: %s\n", state2Str(stateRunning));

让我担心的是该函数返回一个指向临时对象的指针。这种返回值的生命周期是多少?语言为 C89。

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