C++ convert char to const char*

88,249

Solution 1

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

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

Solution 2

You can take the address of that element:

theval = &thestring[i];

Solution 3

Usually a const char * is pointing to a full null-terminated string, not a single character, so I question if this is really what you want to do.

If it's really what you want, the answer is easy:

theval = &thestring[i];

If the function is really expecting a string but you want to pass it a string of a single character, a slightly different approach is called for:

char theval[2] = {0};
theval[0] = thestring[i];
result = func(theval);

Solution 4

I'm guessing that the func call is expecting a C-string as it's input. In which case you can do the following:

string theString = "abc123";
char tempCString[2];
string result;

tempCString[1] = '\0';

for( string::iterator it = theString.begin();
     it != theString.end(); ++it )
{
   tempCString[0] = *it;
   result = func( tempCString );
}

This will produce a small C-string (null terminated array of characters) which will be of length 1 for each iteration.

The for loop can be done with an index (as you wrote it) or with the iterators (as I wrote it) and it will have the same result; I prefer the iterator just for consistency with the rest of the STL.

Another problem here to note (although these may just be a result of generalizing the code) is that the result will be overwritten on each iteration.

Share:
88,249
user1054513
Author by

user1054513

Updated on October 04, 2020

Comments

  • user1054513
    user1054513 over 3 years

    Basically i just 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. heres a example. Thanks for your help.

        string thestring = "abc123";
        const char* theval;
        string result;
    
        for(i = 0; i < thestring.length(); i++){
            theval = thestring[i]; //somehow convert this must be type const char*
            result = func(theval);
        }
    
  • Luchian Grigore
    Luchian Grigore over 12 years
    Since he's already using strings, I'm guessing he's just looking for a pointer to char.
  • Mark Ransom
    Mark Ransom over 12 years
    @LuchianGrigore, it's obvious that the function requires a pointer to char but it's not obvious why. It's trivial to pass a single char as a parameter, so it's far more likely that the function takes in a pointer to a null-terminated string.
  • John Dibling
    John Dibling over 12 years
    Not your downvoter, but this does not answer the question OP actually asked.
  • user1054513
    user1054513 over 12 years
    this is exactly what i needed doing &thestring[i] gave results of abc123, bc123, c123, 123, 23, 3. This gave me each individual character thanks
  • WhatsThePoint
    WhatsThePoint over 6 years
    Could you please format your code by highlighting it and hitting Ctrl+K
  • Max
    Max almost 4 years
    OMG thank you so much. For some reason none of the other answers are working lol