由于 string 和 char 数组的数据类型不同,不能直接进行赋值或转换。可以通过以下方式将 string 转换为 char 数组:

  1. 使用 c_str() 函数将 string 转换为 const char* 指针。

例如:

string str = 'Hello';
const char* cstr = str.c_str();
  1. 将 const char* 指针中的字符复制到 char 数组中。

例如:

string str = 'Hello';
const char* cstr = str.c_str();
char wTitle[70];
strcpy(wTitle, cstr);

注意:需要保证 char 数组 wTitle 的长度足够存储 const char* 指针中的字符,否则可能出现内存溢出的情况。

C++ string 转换为 char 数组的技巧

原文地址: https://www.cveoy.top/t/topic/oOqY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录