这是正确的还是有更好的方法来做到这一点。
Visual Studio给出错误说'strcpy()折旧'。在C++中输入字符串到动态分配的char内存中
using namespace std;
char* ptr;
ptr=(char *)calloc(1,sizeof(char));
cout << "Input the equation." << endl;
string eqn;
getline(cin, eqn);
ptr = (char *)realloc(ptr, top + eqn.size()+1);
strcpy(ptr, eqn.c_str());
P.S.我想ptr是输入方程的确切大小。
为什么地球上您使用的C字符串,C++代码中的'strcpy'和'malloc'?为自己节省很多痛苦,并使用'std :: string'。 – 2014-09-13 16:10:19
折旧?显然微软讨厌它...... – texasbruce 2014-09-13 16:19:40
你已经在你的代码中使用了'std :: string'。你为什么还使用'char *''calloc'和'realloc'?是否因为你认为你需要使用它们是因为一个函数需要传递一个'char *'? – PaulMcKenzie 2014-09-13 16:20:58