Home:ALL Converter>How can I convert const char* to string and then back to char*?

How can I convert const char* to string and then back to char*?

Ask Time:2015-09-23T04:39:10         Author:Arya Atighehchian

Json Formatter

I'm just starting c++ and am having difficulty understanding const char*. I'm trying to convert the input in the method to string, and then change the strings to add hyphens where I want and ultimately take that string and convert it back to char* to return. So far when I try this it gives me a bus error 10.

char* getHyphen(const char* input){
    string vowels [12] = {"A","E","I","O","U","Y","a","e","i","o","u","y"};

    //convert char* to string
    string a;
    int i = 0;
    while(input != '\0'){
        a += input[i];
        input++;
        i++;
    }

    //convert a string to char*

    return NULL;
}

Author:Arya Atighehchian,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32726444/how-can-i-convert-const-char-to-string-and-then-back-to-char
bku_drytt :

A: The std::string class has a constructor that takes a char const*, so you simply create an instance to do your conversion.\n\nB: Instances of std::string have a c_str() member function that returns a char const* that you can use to convert back to char const*.\n\nauto my_cstr = \"Hello\"; // A\nstd::string s(my_cstr); // A\n// ... modify 's' ...\nauto back_to_cstr = s.c_str(); // B\n",
2015-09-22T20:40:49
R Sahu :

First of all, you don't need all of that code to construct a std::string from the input. You can just use:\n\nstring a(input);\n\n\nAs far as returning a new char*, you can use:\n\nreturn strdup(a.c_str()); // strdup is a non-standard function but it\n // can be easily implemented if necessary.\n\n\nMake sure to deallocate the returned value.\n\nIt will be better to just return a std::string so the users of your function don't have to worry about memory allocation/deallocation.\n\nstd::string getHyphen(const char* input){\n",
2015-09-22T20:44:42
Christian Hackl :

Don't use char*. Use std::string, like all other here are telling you. This will eliminate all such problems.\n\nHowever, for the sake of completeness and because you want to understand the background, let's analyse what is going on.\n\n\n\n\nwhile(input != '\\0'){\n\n\n\nYou probably mean:\n\nwhile(*input != '\\0') {\n\n\nYour code compares the input pointer itself to \\0, i.e. it checks for a null-pointer, which is due to the unfortunate automatic conversion from a \\0 char. If you tried to compare with, say, 'x' or 'a', then you would get a compilation error instead of runtime crashes.\n\nYou want to dereference the pointer via *input to get to the char pointed to.\n\n\na += input[i];\ninput++;\ni++;\n\n\n\nThis will also not work. You increment the input pointer, yet with [i] you advance even further. For example, if input has been incremented three times, then input[3] will be the 7th character of the original array passed into the function, not the 4th one. This eventually results in undefined behaviour when you leave the bounds of the array. Undefined behaviour can also be the \"bus error 10\" you mention.\n\nReplace with:\n\na += *input;\ninput++;\ni++;\n\n\n(Actually, now that i is not used any longer, you can remove it altogether.)\n\n\n\nAnd let me repeat it once again: Do not use char*. Use std::string.",
2015-09-22T21:00:35
yy