C++ 将字符串输入到c+中动态分配的char内存中+;

C++ 将字符串输入到c+中动态分配的char内存中+;,c++,string,dynamic,char,C++,String,Dynamic,Char,这是正确的还是有更好的方法来做到这一点。 Visual Studio给出了一个错误,说明“strcpy()已折旧”” 使用名称空间std; char*ptr; ptr=(char*)calloc(1,sizeof(char)); coutstrcpy被弃用,因为它是缓冲区溢出问题的常见来源,通常通过strncpy修复。话虽如此,您最好首先使用std::string。strcpy不受欢迎,因为它是缓冲区溢出问题的常见来源,通常通过strncpy解决。话虽如此,您最好首先使用std::string。

这是正确的还是有更好的方法来做到这一点。
Visual Studio给出了一个错误,说明“strcpy()已折旧”

使用名称空间std;
char*ptr;
ptr=(char*)calloc(1,sizeof(char));

cout
strcpy
被弃用,因为它是缓冲区溢出问题的常见来源,通常通过
strncpy
修复。话虽如此,您最好首先使用
std::string

strcpy
不受欢迎,因为它是缓冲区溢出问题的常见来源,通常通过
strncpy
解决。话虽如此,您最好首先使用
std::string

strcpy
不受欢迎,因为它是缓冲区溢出问题的常见来源,通常通过
strncpy
解决。话虽如此,您最好首先使用
std::string

strcpy
不受欢迎,因为它是缓冲区溢出问题的常见来源,通常通过
strncpy
解决。话虽如此,您最好首先使用
std::string

假设您试图实现的是在给定
std::string
的情况下创建一个
modifiable
字符缓冲区,那么更好的选择是使用
std::vector
来创建这样的缓冲区

#include <vector>
#include <string>
#include <iostream>
//...
void foo(char *x)
{
   // do something to 'x'
}

using namespace std;

int main()
{
    cout << "Input the equation." << endl;
    string eqn;
    getline(cin, eqn);

    // construct vector with string
    std::vector<char> ptr(eqn.begin(), eqn.end());

    // add null terminator
    ptr.push_back(0);

    foo( &ptr[0] );
}
#包括
#包括
#包括
//...
void foo(char*x)
{
//对“x”做点什么
}
使用名称空间std;
int main()
{

cout假设您试图实现的是在给定
std::string
的情况下创建一个
modifiable
char缓冲区,更好的选择是使用
std::vector
创建这样的缓冲区

#include <vector>
#include <string>
#include <iostream>
//...
void foo(char *x)
{
   // do something to 'x'
}

using namespace std;

int main()
{
    cout << "Input the equation." << endl;
    string eqn;
    getline(cin, eqn);

    // construct vector with string
    std::vector<char> ptr(eqn.begin(), eqn.end());

    // add null terminator
    ptr.push_back(0);

    foo( &ptr[0] );
}
#包括
#包括
#包括
//...
void foo(char*x)
{
//对“x”做点什么
}
使用名称空间std;
int main()
{

cout假设您试图实现的是在给定
std::string
的情况下创建一个
modifiable
char缓冲区,更好的选择是使用
std::vector
创建这样的缓冲区

#include <vector>
#include <string>
#include <iostream>
//...
void foo(char *x)
{
   // do something to 'x'
}

using namespace std;

int main()
{
    cout << "Input the equation." << endl;
    string eqn;
    getline(cin, eqn);

    // construct vector with string
    std::vector<char> ptr(eqn.begin(), eqn.end());

    // add null terminator
    ptr.push_back(0);

    foo( &ptr[0] );
}
#包括
#包括
#包括
//...
void foo(char*x)
{
//对“x”做点什么
}
使用名称空间std;
int main()
{

cout假设您试图实现的是在给定
std::string
的情况下创建一个
modifiable
char缓冲区,更好的选择是使用
std::vector
创建这样的缓冲区

#include <vector>
#include <string>
#include <iostream>
//...
void foo(char *x)
{
   // do something to 'x'
}

using namespace std;

int main()
{
    cout << "Input the equation." << endl;
    string eqn;
    getline(cin, eqn);

    // construct vector with string
    std::vector<char> ptr(eqn.begin(), eqn.end());

    // add null terminator
    ptr.push_back(0);

    foo( &ptr[0] );
}
#包括
#包括
#包括
//...
void foo(char*x)
{
//对“x”做点什么
}
使用名称空间std;
int main()
{

cout如果您想使用
malloc
复制字符串,只需使用
strdup

char* ptr = strdup(eqn.c_str());
// ..
free(ptr);

如果您想使用
malloc
复制字符串,只需使用
strdup

char* ptr = strdup(eqn.c_str());
// ..
free(ptr);

如果您想使用
malloc
复制字符串,只需使用
strdup

char* ptr = strdup(eqn.c_str());
// ..
free(ptr);

如果您想使用
malloc
复制字符串,只需使用
strdup

char* ptr = strdup(eqn.c_str());
// ..
free(ptr);


在C++代码中,你为什么使用C字符串,<代码> StrcPy < /C>和<代码> MalOC ?????使用< <代码> STD::Stry< /Cal> >?显然,微软讨厌它……你已经在代码中使用<代码> STD::String 。为什么你还使用<代码> char *<代码> <代码> CaloC,和<代码> ReLoC/?是否因为您认为需要使用它们是因为函数需要向其传递
char*
?为什么要分配内存,然后几乎立即重新分配它?跳过第2行和第3行,并将第7行替换为
char*ptr=(char*)alloc(top+eqn.size()+1)除非使用了库,否则你应该使用<代码> STD::String < /Cord>,而不是使用C语言,除非你使用C字符串,<代码> StrcPy < /C> >和<代码> MalOC < /C>在C++代码中???????您的代码中已经有字符串了。为什么还要使用
char*
calloc
realloc
?是因为您认为需要使用它们,因为函数希望将
char*
传递给它?为什么要分配内存,然后几乎立即重新分配它?跳过第2行、第3行,用h
char*ptr=(char*)alloc(top+eqn.size()+1)除非使用了库,否则你应该使用<代码> STD::String < /Cord>,而不是使用C语言,除非你使用C字符串,<代码> StrcPy < /C> >和<代码> MalOC < /C>在C++代码中???????您的代码中已经有字符串了。为什么还要使用
char*
calloc
realloc
?是因为您认为需要使用它们,因为函数希望将
char*
传递给它?为什么要分配内存,然后几乎立即重新分配它?跳过第2行、第3行,用h
char*ptr=(char*)alloc(top+eqn.size()+1)除非使用了库,否则你应该使用<代码> STD::String < /Cord>,而不是使用C语言,除非你使用C字符串,<代码> StrcPy < /C> >和<代码> MalOC < /C>在C++代码中???????您的代码中已经有字符串了。为什么还要使用
char*
calloc
realloc
?是因为您认为需要使用它们,因为函数希望将
char*
传递给它?为什么要分配内存,然后几乎立即重新分配它?跳过第2行、第3行,用h
char*ptr=(char*)alloc(顶部+