Open In App

Convert String to Char Array in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Here, we will build a C++ program to convert strings to char arrays. Many of us have encountered the error ‘cannot convert std::string to char[] or char* data type’ so let’s solve this using 5 different methods:

  1. Using c_str() with strcpy()
  2. Using c_str() without strcpy()
  3. Using for loop
  4. Using the address assignment of each other method
  5. Using data() (C++17 and newer)

Input:

string s = "geeksforgeeks";

Output:

char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's', '\0' };

1. Using c_str() with strcpy()

A way to do this is to copy the contents of the string to the char array. This can be done with the help of the c_str() and strcpy() functions of library cstring
The c_str() function is used to return a pointer to an array that contains a null-terminated sequence of characters representing the current value of the string.

const char* c_str() const;

If there is an exception thrown then there are no changes in the string. But when we need to find or access the individual elements then we copy it to a char array using strcpy() function. After copying it, we can use it just like a simple array. The length of the char array taken should not be less than the length of an input string.

In order to create a new array to contain the characters, we must dynamically allocate the char array with new. We also must remember to use delete[] when we are done with the array. This is done because, unlike C, C++ does not support Variable Length Arrays (VLA) on the stack.

Example:

C++




// C++ program to convert string
// to char array using c_str()
#include <cstring>
#include <string>
#include <iostream>
  
// driver code
int main()
{
    // assigning value to string s
    std::string s = "geeksforgeeks";
  
    const int length = s.length();
  
    // declaring character array (+1 for null terminator)
    char* char_array = new char[length + 1];
  
    // copying the contents of the
    // string to char array
    strcpy(char_array, s.c_str());
  
    for (int i = 0; i < length; i++)
    {
        std::cout << char_array[i];
    }
    
    delete[] char_array;
    
    return 0;
}


Output

geeksforgeeks
  • Time complexity: O(n)
  • Auxiliary Space: O(1)

2. Using c_str() without strcpy()

An alternate way of Method 1 can be such, without using strcpy() function. This option will not create a new string.

Example:

C++




// C++ program to convert string
// to char array Using c_str() 
// without strcpy()
#include <cstring>
#include <string>
#include <iostream>
  
// driver code
int main()
{
    // assigning value to string s
    std::string s = "GeeksForGeeks";
    
    // the c_str() function returns
    // a const pointer to null 
    // terminated contents.
    const char* str = s.c_str();
    
    // printing the char array
    std::cout << str;
    
    return 0;
}


Output

GeeksForGeeks
  • Time complexity: O(n)
  • Auxiliary Space: O(1)

3. Using for loop

We can use for loop to iterate through each element of the std::string and assign the character to the char array one by one.

Example:

C++




// C++ program to convert string
// to char array Using for loop
#include <iostream>
#include <string>
  
// driver code
int main()
{
    // assigning value to string s
    std::string s = "GeeksForGeeks";
    
    // create a new array of chars to copy to (+1 for a null terminator)
    char* char_array = new char[s.length() + 1];
    
    // make sure that the new string is null terminated
    char_array[s.length()] = '\0';
      
    for (int i = 0; i < s.length(); i++) {
        char_array[i] = s[i];
    }
  
    std::cout << char_array;
  
    delete[] char_array;
  
    return 0;
}


Output

GeeksForGeeks
  • Time complexity: O(n)
  • Auxiliary Space: O(1)

4. Using the address assignment of each other method

This is the simplest and most efficient one. We can directly assign the address of 1st character of the string to a pointer to the char. This should be the preferred method unless your logic needs a copy of the string.  

Example:

C++




// C++ program to convert string
// to char array Using the address
// assignment of each other method
#include <iostream>
#include <string>
  
// Driver Code
int main()
{
    std::string s = "GeeksForGeeks";
  
    char* char_arr = &s[0];
      
    std::cout << char_arr;
      
    return 0;
}


Output

GeeksForGeeks
  • Time complexity: O(n)
  • Auxiliary Space: O(1)

5. Using .data() (with C++17 or newer)

Using .data() to access a non-const char* of the std::string is the best option in C++17 or newer. Note: This can’t be run in the GeeksforGeeks IDE because it doesn’t support C++17.

Example:

C++




// C++ program to convert string
// to char array using .data()
#include <iostream>
#include <string>
  
// Driver Code
int main()
{
    std::string s = "GeeksForGeeks";
  
    char* char_arr = s.data();
  
    std::cout << char_arr;
  
    return 0;
}


Output

GeeksForGeeks
  • Time complexity: O(n)
  • Auxiliary Space: O(1)


Last Updated : 07 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads