Search code examples
c++stringcharconstants

How to convert a single char to a null-terminated const char*


I want to loop through a string of characters pull each one out and each one has to be of type const char* so I can pass it to a function. Here's an example.

string thestring = "abc123";

for(int i = 0; i < thestring.length(); i++){
    const char* theval = thestring[i]; // somehow convert this must be type const char*
    string result = func(theval);
    // ...
}

Solution

  • string sym(1, thestring[i]);
    theval = sym.c_str();
    

    It gives a null-terminated const char* for every character.