转换字符数组字符串采用C

编程入门 行业动态 更新时间:2024-10-25 16:26:58
本文介绍了转换字符数组字符串采用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一个char数组转换为字符串。事情是这样的:

字符数组[20];字符字符串[100];数组[0] ='1';阵列[1] =7;阵列[2] =8;阵列[3] ='。';阵列[4] ='9';...

我想获得类似的东西:

字符字符串[0] =数组//它被存放178.9 ....位置[0]

解决方案

你是说你有这样的:

字符数组[20];字符字符串[100];数组[0] ='1';阵列[1] =7;阵列[2] =8;阵列[3] ='。';阵列[4] ='9';

和你想有这样的:

串[0] =178.9; //它被存储178.9 ....在位置[0]

您不能有。一个char拥有1个字符。而已。A串在C是字符数组后跟一个标记字符(NULL结束符)。

现在,如果你想第一个X字符出阵列复制到字符串你可以做到这一点与的memcpy():

的memcpy(字符串,数组,X);字符串[X] ='\\ 0';

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

I would like to get something like that:

char string[0]= array // where it was stored 178.9 ....in position [0]

解决方案

You're saying you have this:

char array[20]; char string[100]; array[0]='1'; array[1]='7'; array[2]='8'; array[3]='.'; array[4]='9';

And you'd like to have this:

string[0]= "178.9"; // where it was stored 178.9 ....in position [0]

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).

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

本文发布于:2023-11-04 05:43:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!