C++ Converting a char to a const char *

44,021

Solution 1

char a = 'z';
const char *b = &a;

Of course, this is on the stack. If you need it on the heap,

char a = 'z';
const char *b = new char(a);

Solution 2

If the function expects a const pointer to an exiting character you should go with the answer of Paul Draper. But keep in mind that this is not a pointer to a null terminated string, what the function might expect. If you need a pointer to null terminated string you can use std::string::c_str

f(const char* s);

char a = 'z'; 
std::string str{a};
f(str.c_str());
Share:
44,021
Admin
Author by

Admin

Updated on December 13, 2020

Comments

  • Admin
    Admin over 3 years

    I'm trying to use beginthreadex to create a thread that will run a function that takes a char as an argument. I'm bad at C++, though, and I can't figure out how to turn a char into a const char , which beginthreadex needs for its argument. Is there a way to do that? I find a lot of questions for converting a char to a const char, but not to a const char *.

  • filmor
    filmor over 10 years
    The question is about C++, so you might want to go with new instead.