-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToCharArray.cpp
More file actions
33 lines (30 loc) · 932 Bytes
/
StringToCharArray.cpp
File metadata and controls
33 lines (30 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*---------------------
作者:dongfanglanyi
来源:CSDN
原文:https://blog.csdn.net/dongfanglanyi/article/details/82355982
版权声明:本文为博主原创文章,转载请附上博文链接!
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char *argv[])
{
string str = "hello world!";
char *buff;
const char * cstr = str.data();
const char *cstr2 = str.c_str();
//string::npos是机器上最大的正整数,常用来表示string的结束位置
size_t length = str.copy(buff,string::npos,0);
buff[length] = '\0';
for(int i =0; str[i]!='\0'; i++)
cout << str[i];
cout <<"data():" << cstr << endl;
cout << "c_str():" << cstr2 << endl;
cout << "copy():" << buff << endl;
str = "chinese";
cout <<"data():" << cstr << endl;
cout << "c_str():" << cstr2 << endl;
cout << "copy():" << buff << endl;
return 0;
}