Many of us have encountered error ‘cannot convert std::string to char[] or char* data type’.
Examples:
Input : string s = "geeksforgeeks" ;
Output : char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's' } ;
Input : string s = "coding" ;
Output : char s[] = { 'c', 'o', 'd', 'i', 'n', 'g' } ;
Method 1
A way to do this is to copy the contents of the string to char array. This can be done with the help of c_str() and strcpy() function.
The c_str() function is used to return a pointer to an array that contains a null terminated sequence of character representing the current value of the string.
Syntax:
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 input string.
CPP
// CPP program to convert string// to char array#include <bits/stdc++.h>using namespace std;// driver codeint main(){ // assigning value to string s string s = "geeksforgeeks"; int n = s.length(); // declaring character array char char_array[n + 1]; // copying the contents of the // string to char array strcpy(char_array, s.c_str()); for (int i = 0; i < n; i++) cout << char_array[i]; return 0;} |
Output:
geeksforgeeks
Method 2:
CPP
// CPP program to convert string// to char array#include <iostream>#include <string.h>using namespace std;// driver codeint main(){ // assigning value to string s string s("geeksforgeeks"); // declaring character array : p char p[s.length()]; int i; for (i = 0; i < sizeof(p); i++) { p[i] = s[i]; cout << p[i]; } return 0;} |
Output:
geeksforgeeks
Method 3:
This is the simplest ad most efficient one. We can directly assign the address of 1st character of the string to a pointer to char. This should be the preferred method unless your logic needs a copy of the string.
C++14
// CPP program for the above approach#include <cstring>#include <iostream>#include <string>using namespace std;// Driver Codeint main(){ char* char_arr; string str_obj("GeeksForGeeks"); char_arr = &str;_obj[0]; cout << char_arr; return 0;} |
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- Difference between const char *p, char * const p and const char * const p
- char* vs std:string vs char[] in C++
- What's difference between char s[] and char *s in C?
- Move all special char to the end of the String
- Convert string X to an anagram of string Y with minimum replacements
- Convert the string into palindrome string by changing only one character.
- Convert Hexadecimal value String to ASCII value String
- Minimum number of given operations required to convert a string to another string
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Wide char and library functions in C++
- Maximum length palindromic substring such that it starts and ends with given char
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Convert a String to Integer Array in C/C++
- Convert character array to string in C++
- Convert Character Array To String In Java
- Convert a floating point number to string in C
- How to convert C style strings to std::string and vice versa?
- Convert to a string that is repetition of a substring of k length
- Convert characters of a string to opposite case
- Minimum reduce operations to convert a given string into a palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

