- 因为string可以看作是数组构成的串,所以直接定义一个char的指针,指过去就可以了。
示例如下:
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
string s1 = "abcdeg";
const char *k;
k = s1.c_str();
cout << k[0] << endl;
system("pause");
return 0;
}
- 比较机械的,先定义一个字符数组,然后将字串的内容“复制”进去。这种方法更规矩一些,也更安全一些:
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
string s = "a1234";
char c[20];
strcpy(c, s.c_str());
cout << c[0] << endl;
system("pause");
return 0;
}