I want to assign string from command line. But I got segmentation fault. why? here is the code:
char* ans;
cout << "enter string: ";
cin >> *ans;
update:
how to allocate memory in advance if I don’t know the size of input string,
how it works in String Class?
You have a pointer, but that pointer doesn’t point to anywhere where there’s space allocated for data to be stored. Use
std::stringinstead: