How to Convert string to char* in C++

Are you looking to know how you can convert string to char* in C++? In this post, I will show you various ways you can use to convert the given string to a char array in C++.

There are already various STL templates present in the string class of C++ that you can use to convert it to the char array of pointer and set the last element of the array in char to the null value.

You can achieve this by using the below functions or methods in C++.

  • Using c_str() function Of String Class.
  • Using std::copy Allows you to edit the String.
  • Using std::string::data function of string class
  • Using std::vector instead of Char*

Now let us see each method mentioned above in detail with code examples.

Using c_str() function of String Class

Returns a pointer to an array containing a null-terminated sequence of characters (a C-string) representing the current value of the string object.

This array contains the same character sequence as the string[1] object’s value, plus a null character (‘0’) at the end.

// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>

int main ()
{
  std::string str  = "This is the char array that we want to convert";
  
  //creating a char array with size of the string plus one
  const char* convertString = str.c_str();
  
  std::cout<<convertString;

  return 0;
}

Output:

This is the char array that we want to convert

Using std::copy Allows you to edit the String

Copies a substring from the current value of the String object into the array pointed by s. This substring contains the number of characters to copy that begin at the position of the first character.

The function does not end the copied content with a null character. As a result, you must include the null (‘\0’) character at the end of the character array.

// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>

int main ()
{
  std::string str  = "This is the char array that we want to convert";
  
  //creating a char array with size of the string plus one
  char* convertString = new char[str.size()+1];
  
  std::copy(str.begin(), str.end(), convertString);
  
  convertString[str.size()] = '\0';
  
  std::cout<<convertString;

  return 0;
}

Output:

This is the char array that we want to convert

Using std::string::data function of string class

Returns a pointer to an array that contains the same sequence of characters as the string object’s value.

When you use data()+size() to access the value, you get the following undefined behavior: There is no guarantee that a null character will end the character sequence referred to by the value of this function. String::c_str is a function that provides such a guarantee.

What you get is essentially an immutable character array.

// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>

int main ()
{
  std::string str  = "This is the char array that we want to convert";
  
  //creating a char array with size of the string plus one
  const char* convertString = str.data();
  
  std::cout<<convertString;

  return 0;
}

Output:

This is the char array that we want to convert
How to Convert string to char array in C++

Using std::vector instead of Char*

This is the accepted method. Because vector is the most advanced version of an array in the most recent C++ programming language. You can get the same results by copying string data to vector instead of char. The char array is then used to address the vector’s first element.

// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>
#include <vector>

int main ()
{
  std::string str  = "This is the char array that we want to convert";
  
  //creating a vector of char with size of the string plus one
  std::vector<char> newWritableString(str.begin(), str.end());
  newWritableString.push_back('\0');

//Create Char* array and assign it to initial address of vector array
const char* convertString = &newWritableString[0];
  
  std::cout<<convertString;

  return 0;
}

Output:

How to Convert string to char* in C++

Wrap Up

I hope you were able to convert string to char* in C++, or string to a char array. With code examples, I’ve listed around four methods for converting a string to a char array in C++. Please let me know in the comments section if you know of a better method than the one described above.

If you liked our post then follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Check If String is Palindrome Using Recursion

Leave a Comment