如何在c++中转换字符串到char数组?

时间:2022-08-28 16:22:27

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?

我想要将字符串转换为char数组,而不是char*。我知道如何将字符串转换为char*(通过使用malloc或我在代码中发布的方式)——但这不是我想要的。我只是想将字符串转换成char[size]数组。是可能的吗?

#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;
}

10 个解决方案

#1


91  

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

#2


37  

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数组所以常数是不确定的,然后继续。

    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.

这两个都是作业,大部分时间都是你需要的,如果你真的需要一份新的副本,那就跟着其他人的答案。

#3


15  

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

#4


8  

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.

在c++中避免C是更好的做法,所以std:::copy应该是选择,而不是strcpy。

#5


6  

Just copy the string into the array with strcpy.

只需将字符串复制到带有strcpy的数组中。

#6


5  

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'; 

#7


4  

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

试试strcpy(),但是正如Fred所说,这是c++,而不是C。

#8


2  

You could use strcpy(), like so:

可以使用strcpy(),比如:

strcpy(tab2, tmp.c_str());

Watch out for buffer overflow.

注意缓冲区溢出。

#9


1  

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:

如果您事先不知道字符串的大小,并且它可能会有很大的变化,那么您可以使用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.

注意,这里不需要strncpy,因为数组在一开始就被分配得足够大。

#10


0  

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

#1


91  

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

#2


37  

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数组所以常数是不确定的,然后继续。

    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.

这两个都是作业,大部分时间都是你需要的,如果你真的需要一份新的副本,那就跟着其他人的答案。

#3


15  

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

#4


8  

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.

在c++中避免C是更好的做法,所以std:::copy应该是选择,而不是strcpy。

#5


6  

Just copy the string into the array with strcpy.

只需将字符串复制到带有strcpy的数组中。

#6


5  

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'; 

#7


4  

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

试试strcpy(),但是正如Fred所说,这是c++,而不是C。

#8


2  

You could use strcpy(), like so:

可以使用strcpy(),比如:

strcpy(tab2, tmp.c_str());

Watch out for buffer overflow.

注意缓冲区溢出。

#9


1  

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:

如果您事先不知道字符串的大小,并且它可能会有很大的变化,那么您可以使用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.

注意,这里不需要strncpy,因为数组在一开始就被分配得足够大。

#10


0  

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