How to convert string to char array in C++?

C++StringType Conversion

C++ Problem Overview


I would like to convert string to char array but not char*. I know how to convert string to char* (by using malloc or the way I posted it in my code) - but that's not what I want. I simply want to convert string to char[size] array. Is it possible?

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

int main()
{
    // char to string
    char tab[4];
    tab[0] = 'c';
    tab[1] = 'a';
    tab[2] = 't';
    tab[3] = '\0';
    string tmp(tab);
    cout << tmp << "\n";

    // string to char* - but thats not what I want

    char *c = const_cast<char*>(tmp.c_str());
    cout << c << "\n";

    //string to char
    char tab2[1024];
    // ?

    return 0;
}

C++ Solutions


Solution 1 - C++

Simplest way I can think of doing it is:

string temp = "cat";
char tab2[1024];
strcpy(tab2, temp.c_str());

For safety, you might prefer:

string temp = "cat";
char tab2[1024];
strncpy(tab2, temp.c_str(), sizeof(tab2));
tab2[sizeof(tab2) - 1] = 0;

or could be in this fashion:

string temp = "cat";
char * tab2 = new char [temp.length()+1];
strcpy (tab2, temp.c_str());

Solution 2 - C++

Ok, i am shocked that no one really gave a good answer, now my turn. There are two cases;

  1. A constant char array is good enough for you so you go with,

    const char *array = tmp.c_str();
    
  2. Or you need to modify the char array so constant is not ok, then just go with this

    char *array = &tmp[0];
    

Both of them are just assignment operations and most of the time that is just what you need, if you really need a new copy then follow other fellows answers.

Solution 3 - C++

str.copy(cstr, str.length()+1); // since C++11
cstr[str.copy(cstr, str.length())] = '\0';  // before C++11
cstr[str.copy(cstr, sizeof(cstr)-1)] = '\0';  // before C++11 (safe)

It's a better practice to avoid C in C++, so std::string::copy should be the choice instead of strcpy.

Solution 4 - C++

Easiest way to do it would be this

std::string myWord = "myWord";
char myArray[myWord.size()+1];//as 1 char space for null is also required
strcpy(myArray, myWord.c_str());

Solution 5 - C++

Just copy the string into the array with strcpy.

Solution 6 - C++

Try this way it should be work.

string line="hello world";
char * data = new char[line.size() + 1];
copy(line.begin(), line.end(), data);
data[line.size()] = '\0'; 

Solution 7 - C++

Try strcpy(), but as Fred said, this is C++, not C

Solution 8 - C++

You could use strcpy(), like so:

strcpy(tab2, tmp.c_str());

Watch out for buffer overflow.

Solution 9 - C++

If you don't know the size of the string beforehand, you can dynamically allocate an array:

auto tab2 = std::make_unique<char[]>(temp.size() + 1);
std::strcpy(tab2.get(), temp.c_str());

Solution 10 - C++

If you're using C++11 or above, I'd suggest using std::snprintf over std::strcpy or std::strncpy because of its safety (i.e., you determine how many characters can be written to your buffer) and because it null-terminates the string for you (so you don't have to worry about it). It would be like this:

#include <string>
#include <cstdio>

std::string tmp = "cat";
char tab2[1024];
std::snprintf(tab2, sizeof(tab2), "%s", tmp.c_str());

In C++17, you have this alternative:

#include <string>
#include <cstdio>
#include <iterator>

std::string tmp = "cat";
char tab2[1024];
std::snprintf(tab2, std::size(tab2), "%s", tmp.c_str());

Solution 11 - C++

Well I know this maybe rather dumb than and simple, but I think it should work:

string n;
cin>> n;
char b[200];
for (int i = 0; i < sizeof(n); i++)
{
	b[i] = n[i];
	cout<< b[i]<< " ";
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian BrownView Question on Stackoverflow
Solution 1 - C++ChowlettView Answer on Stackoverflow
Solution 2 - C++radView Answer on Stackoverflow
Solution 3 - C++YoukaView Answer on Stackoverflow
Solution 4 - C++user2963410View Answer on Stackoverflow
Solution 5 - C++David SchwartzView Answer on Stackoverflow
Solution 6 - C++Tharindu DhanushkaView Answer on Stackoverflow
Solution 7 - C++WaterfragView Answer on Stackoverflow
Solution 8 - C++Fred LarsonView Answer on Stackoverflow
Solution 9 - C++emlaiView Answer on Stackoverflow
Solution 10 - C++luizflsView Answer on Stackoverflow
Solution 11 - C++SquircKleView Answer on Stackoverflow