码迷,mamicode.com
首页 > 编程语言 > 详细

C++字符串转换为C语言字符串

时间:2018-06-02 00:28:50      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:IV   clu   return   字符   两种   方法   span   print   %s   

C++中string类,有data(),c_str()两种方法返回const char *类型指针,指向C语言类型的字符串。

注意,一定要用strcpy来操作。

#include <stdio.h>
#include <string.h>
#include <string>

int main() {
  std::string str = "123abc";
  char cstr[20] = "";
  strcpy(cstr, str.data());

  printf("%s\n", cstr);
  return 0;
}

 

C++字符串转换为C语言字符串

标签:IV   clu   return   字符   两种   方法   span   print   %s   

原文地址:https://www.cnblogs.com/tangjicheng/p/9123997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!