我们可以在C中返回字符串文字

Rya*_*yan 38 c c++ pointers dangling-pointer

这段代码有效吗?

const char* foo() {
  return "Hello World";
}
Run Code Online (Sandbox Code Playgroud)

也就是说,从C函数返回"Hello World".

const char* str = foo();
Run Code Online (Sandbox Code Playgroud)

str是一个悬垂的指针吗?

PS:上面的函数是从一些实际代码中抽象出来的.我看到有人编写直接返回字符串文字的代码.