本文介绍了使用 C 将字符数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将字符数组转换为字符串.像这样:
I need to convert a char array to string.Something like this:
char array[20];
char string[100];
array[0]='1';
array[1]='7';
array[2]='8';
array[3]='.';
array[4]='9';
...
我想得到这样的东西:
char string[0]= array // where it was stored 178.9 ....in position [0]
推荐答案
你是说你有这个:
char array[20]; char string[100];
array[0]='1';
array[1]='7';
array[2]='8';
array[3]='.';
array[4]='9';
你想要这个:
string[0]= "178.9"; // where it was stored 178.9 ....in position [0]
你不能拥有那个.一个字符包含 1 个字符.就是这样.C 中的字符串"是一个字符数组,后跟一个标记字符(NULL 终止符).
You can't have that. A char holds 1 character. That's it.A "string" in C is an array of characters followed by a sentinel character (NULL terminator).
现在,如果您想将 array
中的前 x 个字符复制到 string
,您可以使用 memcpy()
:>
Now if you want to copy the first x characters out of array
to string
you can do that with memcpy()
:
memcpy(string, array, x);
string[x] = '\0';
这篇关于使用 C 将字符数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!