1. 程式人生 > >C++字符串轉換為C語言字符串

C++字符串轉換為C語言字符串

strcpy .data c++ 類型 stdio.h int ++ abc AS

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語言字符串