arrays - 如何将 int 数组转换为字符串数组

标签 arrays c string char integer

关闭。这个问题需要更多 focused 。它目前不接受答案。












想改善这个问题吗?更新问题,使其仅通过 editing this post 关注一个问题。

6年前关闭。




Improve this question




我有一个整数数组:

int a[5]={5,21,456,1,3}

我需要将这些数字存储到 char 数组中,以便 char 数组具有如下内容:
char *s="52145613";

中是否有用于此的库函数?

最佳答案

sprintf做你需要的。

小例子

char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
   index += sprintf(&str[index], "%d", a[i]);
snprintf处理 str 长度
char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
   index += snprintf(&str[index], 128-index, "%d", a[i]);

关于arrays - 如何将 int 数组转换为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30234363/

相关文章:

python - 将列表的某些字符转换为字符串

c++ - 3维数组的重新分配

C程序不写入文本文件

c# - 在数据表中旋转图像(字节数组)

使用宏检查 C 中的类型大小

c - 如何在Mac OS X的C程序中包含hdf5头文件?

string - 在PLSQL中提取URL路径段字符串

c - 我什么时候应该在 scanf() 中使用&符号

java - System.arrayCopy() 复制对象或对对象的引用?

c++ - 在 C++ 中初始化一个 const 多维数组