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

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

Posted by: admin November 29, 2017 Leave a comment

Questions:

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] = '
#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;
}
'; 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; }
Answers:

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;

Questions:
Answers:

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.

Questions:
Answers:

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());

Questions:
Answers:

Just copy the string into the array with strcpy.

Questions:
Answers:
str.copy(cstr, str.length()+1); // since C++11
cstr[str.copy(cstr, str.length())] = '
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) 
'; // before C++11 cstr[str.copy(cstr, sizeof(cstr)-1)] = '
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) 
'; // 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.

Questions:
Answers:

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

Questions:
Answers:

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()] = '
string line="hello world"; char * data = new char[line.size() + 1]; copy(line.begin(), line.end(), data); data[line.size()] = '\0'; 
';

Questions:
Answers:

You could use strcpy(), like so:

strcpy(tab2, tmp.c_str());

Watch out for buffer overflow.

Questions:
Answers:

If you don’t know the size of the string beforehand and it can vary wildly, you can get a dynamically allocated fixed-size array with the array overload of unique_ptr:

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

Note that you don’t need strncpy here as the array is allocated to be sufficiently large in the first place.

Questions:
Answers:

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]<< " ";
}