[Solved] c++


This line:

des[sizeof(src) + 1];

does nothing. But even if it did something it wouldn’t do what you wanted it to do. First, you’re just referencing a single byte in memory somewhere and not doing anything with it. And secondly, sizeof(src) is not the length of the string you get passed in… it’s the size of the std::string class, which is a constant independent of the length of the actual string.

What you mean to do is actually create a new array:

des = new char[src.size() + 1];

But this will just leak memory, since des is a local variable. What you probably want to do is:

char* toNormalWord(const std::string& src) 
{
    char* des = new char[src.size() + 1];
    // stuff
    return des;
}

Or even better, not use char* to begin with:

std::string toNormalWord(const std::string& src)
{
    std::string des = src;
    // stuff
    return des;
}

0

solved c++