如何将char数组转换为C中的字符串? [重复]

时间:2022-10-30 15:41:05

This question already has an answer here:

这个问题在这里已有答案:

I want to convert my char array to a string so I can pass it into a function. Say if I have this:

我想将我的char数组转换为字符串,以便将其传递给函数。如果我有这个:

char array[3] = {'1', 'a', '/'};

and I want to convert it to

我想把它转换成

char *string = "1a/";

Do I just add a NULL terminator on the end?

我只是在末尾添加一个NULL终结符吗?

3 个解决方案

#1


4  

Declare your array like this

像这样声明你的数组

char array[] = {'1', 'a', '/', '\0'};  

or

要么

char array[] = "1a/";

#2


4  

Just add a zero-delimiter

只需添加零分隔符

char array[4] = {'1', 'a', '/', '\0'};

#3


0  

First of all, a char array and a char pointer are mostly the same, can at times be used interchangeably, but ultimately, a char[] has a known size, making it possible to use the sizeof() function, whereas char* just points to the first address of contiguous memory of unknown length, so sizeof will return whatever your default size for integers are (4 on 32-bit systems, 8 on 64-bit systems) to indicate the size required to store an address.

首先,char数组和char指针大致相同,有时可以互换使用,但最终,char []具有已知大小,可以使用sizeof()函数,而char *只是指向未知长度的连续内存的第一个地址,因此sizeof将返回整数的默认大小(32位系统上4个,64位系统上8个),以指示存储地址所需的大小。

But generally the rule with strings are that they have to be null-terminated, so it doesn't matter if you use char[] or char*. See examples below.

但通常带字符串的规则是它们必须以null结尾,因此如果使用char []或char *则无关紧要。见下面的例子。

char array[4] = {'1', 'a', '/', 0};

OR

要么

char string[4];
memset(&string[0], 0x00, sizeof(string));
memcpy(&string[0], &array[0], 3);

OR

要么

char* string;
string = malloc(sizeof(array)+1);
memset(&string[0], 0x00, sizeof(array)+1);
memcpy(&string[0], &array[0], sizeof(array));

and then passing the string as a char* is as simple as:

然后将字符串作为char *传递如下:

void foo (char* bar, int n)
{
  // do something with bar
}

and call it as

并称之为

foo(&string[0], strlen(string));

Important to note, strlen can only be used on null-terminated char* strings.

需要注意的是,strlen只能用于以null结尾的char *字符串。

If you have any questions, feel free to ask.

如果你有任何问题随时问。

#1


4  

Declare your array like this

像这样声明你的数组

char array[] = {'1', 'a', '/', '\0'};  

or

要么

char array[] = "1a/";

#2


4  

Just add a zero-delimiter

只需添加零分隔符

char array[4] = {'1', 'a', '/', '\0'};

#3


0  

First of all, a char array and a char pointer are mostly the same, can at times be used interchangeably, but ultimately, a char[] has a known size, making it possible to use the sizeof() function, whereas char* just points to the first address of contiguous memory of unknown length, so sizeof will return whatever your default size for integers are (4 on 32-bit systems, 8 on 64-bit systems) to indicate the size required to store an address.

首先,char数组和char指针大致相同,有时可以互换使用,但最终,char []具有已知大小,可以使用sizeof()函数,而char *只是指向未知长度的连续内存的第一个地址,因此sizeof将返回整数的默认大小(32位系统上4个,64位系统上8个),以指示存储地址所需的大小。

But generally the rule with strings are that they have to be null-terminated, so it doesn't matter if you use char[] or char*. See examples below.

但通常带字符串的规则是它们必须以null结尾,因此如果使用char []或char *则无关紧要。见下面的例子。

char array[4] = {'1', 'a', '/', 0};

OR

要么

char string[4];
memset(&string[0], 0x00, sizeof(string));
memcpy(&string[0], &array[0], 3);

OR

要么

char* string;
string = malloc(sizeof(array)+1);
memset(&string[0], 0x00, sizeof(array)+1);
memcpy(&string[0], &array[0], sizeof(array));

and then passing the string as a char* is as simple as:

然后将字符串作为char *传递如下:

void foo (char* bar, int n)
{
  // do something with bar
}

and call it as

并称之为

foo(&string[0], strlen(string));

Important to note, strlen can only be used on null-terminated char* strings.

需要注意的是,strlen只能用于以null结尾的char *字符串。

If you have any questions, feel free to ask.

如果你有任何问题随时问。