1

I am working on a C++ project for a Vex Robot, I am using a function that takes a const char[] but for showing an integer, there is no such function that converts to const char[], so is there a way to do that. All the search results showed how to convert to const char* and by the way "string" is of type const char[].

EDIT: I also need to covert the string to concatenate tow strings (but ended up just coverting both strings to std::string and then back to const char[])

4
  • 1
    A function that takes const char[] will accept const char*. Commented Jul 11, 2021 at 19:24
  • 1
    There is the std::string::c_str() function Commented Jul 11, 2021 at 19:24
  • Does this answer your question? How to convert a std::string to const char* or char* Commented Aug 14, 2021 at 0:21
  • also I found * string.c_str() also works Commented Aug 30, 2021 at 14:18

1 Answer 1

1

With std::string there is the function c_str() (with the synonym data() since C++11) to get a pointer to the underlying null-terminated character array:

std::string const myString{"stackoverflow"}; const char* cstr = myString.c_str(); const char* data = myString.data(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.