【发布时间】:2015-04-10 12:43:14
【问题描述】:
#include<iostream>
using namespace std;
int main()
{
char *a;
cin>>a;
cout<<a;/*when input is string it gives segmentation fault*/
char* b="hello";/*while this thing works fine*/
cout<<b;
}
两者到底有什么区别?
【问题讨论】:
-
cin >> a不正确。 -
区别是第一个不正确
-
你需要分配内存:
char *a = new char[20]; -
不同的是你还没有读过你的C++书。
-
第二个只能正常工作,因为您的编译器已过时。现在禁止从字符串文字到
char*的转换,所以应该是char const *b