getline(cin,_string);

隐身守侯 提交于 2019-12-24 18:35:56

问题


I know that getline(cin,_string); works perfectly

but this dosen't:

char* _chArr = new char;
getline(cin,_chArr);

Even this alson doesn't work:

char* _chArr = new char[30];
getline(cin,_chArr);

Isn't char* a string??


回答1:


Well think of it logically. the char* is just a pointer to a character type memory block. You have to assign it some amount of dynamic memory and then copy data into it using strcpy() or manually. Direct input is not supported in C++. Strings are in fact objects which contain size within themselves. They are designed by the experts in this industry, and they have provided the direct input and dynamic growth as in built functionality. There is a differnce between string and cstring. Cstring is the char*.




回答2:


isn't char* is a string

No, it's a pointer to a char and that's that. The function std::getline does some cool stuff (extending the string and all) that can't be done easily on a char *.




回答3:


No, C++ strings are not just character arrays, they are a full blown class, usually with quite a bit of extra stuff under the covers, over and above what a character array provides.



来源:https://stackoverflow.com/questions/12026541/getlinecin-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!