最好作为输出参数: void testfunc(char* outStr){ char str[10]; for(int i=0; i < 10; ++i){ outStr[i] = str[i]; } } 调用 int main(){ char myStr[10]; testfunc(myStr); // myStr is now filled } 原文由 Xeo 发布,翻译遵循 CC BY-SA 3.0 许可协议
使用 c++17,您可以使用下一个代码: auto testfunc() { union { char str[14]; } res = { .str = "Hello, World!" }; return res; } 然后用你的字符串作为 const auto str = testfunc(); std::cout << str.str; 原文由 magrif 发布,翻译遵循 CC BY-SA 4.0 许可协议
最好作为输出参数:
调用