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 setting the last element of the array in char to the null value.

Using c_str() function of String Class

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

This array has the same sequence of characters 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 string object’s current value into the array pointed by s. The Number of characters to copy that begins at the position of the first character is contained in this substring.

The copied content is not terminated with a null character by the function. Hence You need to add 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 containing the same sequence of characters as the characters in the string object’s value.

When you access the value at data()+size(), you get the following undefined behavior: There’s no guarantee that a null character will end the character sequence referenced by this function’s value. A function that provides such a guarantee is string::c_str.

It is basically an immutable character array is what you get

// 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

Using std::vector instead of Char*

This is the standard way. Since vector is the most advanced version of the array in the latest C++ programming language. You can copy the data of string to vector instead of char* and get the same features. And then assign the char* array to address the first element of the vector.

// 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++

If you liked our post related to how you can convert string to char* in C++ then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Also, subscribe to our newsletter to get notifications for the new posts.

Further Read:

  1. Check If String is Palindrome Using Recursion
Share your love

One comment

Leave a Reply