我如何将一个int数组存储到字符串中 [英] how can i store an int array into string

查看:87
本文介绍了我如何将一个int数组存储到字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数数组:

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

我需要将这些数字存储到 char 数组中,以便char数组具有以下内容:

I need to store these number into char array so that the char array will have some thing like this:

char *s="52145613";

的问题中是否有任何库函数这个吗?

Is there any library function in c for this?

推荐答案

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

这篇关于我如何将一个int数组存储到字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆