一个简单的C++问题,麻烦大家了
#include <iostream>
#include <string>
using namespace std;
int main(){
cout < < "input string " < <endl;
string a;
cin> > a;
char *t=a;
for(int i=0;i <5;i++)
cout < <*(t+i) < < " ";
return 0;
}
为什么编译不能通过啊,麻烦大家了啊。好像是为什么不能指针不能指向数组a
[解决办法]
string a;
cin> > a;
char *t=a;
-----------
这边不是数组。 a 是一个对象。string类 定义的对象。
指向字符的指针 指向 对象。编译就通不过了。
a.c_str()返回一个它自己的使用内存的地址.