我的输入为[5] = {1,2,3,4,5)} 我的输出应该是b = 12345
我尝试使用以下程序,但是我在遍历Char指针时得到了结构,任何人都可以为我的代码提供正确的解决方案..
附上示例代码,
int main()
{
int a[5] ={1,2,3,4,5};
int i ;
char *b;
for(i=0 ; i<5 ; i++)
{
sprintf(b,"%d",a[i]);
b= b+1;
}
printf("\n %s",b);
return 0;
}
答案 0 :(得分:1)
直接的方法很简单:
#include <ansi_c.h>//collection of all ansi C headers files in my environment.
//replace this with appropriate headers supporting your environment
int main()
#define MAX_DIGIT_LEN 10 //memory allocation for char *b
{
int a[5] ={1,2,3,4,5};
int i ;
char *b; //in your original post, you never set
//this pointer to point to any space
//it was therefore an uninitialized
//pointer, with no memory space.
//you must initialize pointers, and allocate some memory.
//Somthing like this will work:
//memory must be MAX_DIGIT_LEN * number of ints in array
b = malloc(MAX_DIGIT_LEN*(sizeof(a)/sizeof(a[0]))+1);//using "sizeof(a)/sizeof(a[0]"
//will accommodate array size changes
//+1 allows for NULL byte at end of char array
sprintf(b, "%d%d%d%d%d", a[0],a[1],a[2],a[3],a[4]);
printf("\n %s",b);
free(b);
return 0;
}
生成此输出:
如果您想使用循环 ,请考虑使用(创建)其他变量:
#include <ansi_c.h>//collection of all ansi C headers files in my environment.
//replace this with appropriate headers supporting your environment
#define MAX_DIGIT_LEN 10 //memory allocation for char *b
int main()
{
int a[5] ={1,2,3,4,5};
int i ;
char *b;
char buf[MAX_DIGIT_LEN];
//create this as an intermediary place
//to store discrete elements of a[]
//arbitrarily chose 10, to hold up to
//9 digit integer, such as 123456789.
//if you anticipate larger digits,
//use a bigger index to create buf[]
//memory must be MAX_DIGIT_LEN * number of ints in array
b = calloc(MAX_DIGIT_LEN*(sizeof(a)/sizeof(a[0]))+1, 1);
//note calloc (rather than malloc) is
//preferable here to initialize
//all memory space with 0 before
//attempting strcat() calls.
for(i=0 ; i<sizeof(a)/sizeof(a[0]) ; i++)
{
sprintf(buf, "%d", a[i]);
strcat(b,buf);
}
printf("\n %s",b);
free(b);
return 0;
}
生成此输出:
答案 1 :(得分:0)
您需要正确设置目的地:
int main()
{
int a[5] ={1,2,3,4,5};
int i ;
char b[6] = "";
char *pb = b;
for(i=0 ; i<5 ; i++)
{
sprintf(pb,"%d",a[i]);
pb++;
}
printf("\n %s",b);
return 0;
}
或没有sprintf
int main()
{
int a[5] ={1,2,3,4,5};
int i ;
char b[6] = "";
for(i=0 ; i<5 ; i++)
{
b[i] = a[i]+48; //Conversion to ASCII
}
printf("\n %s",b);
return 0;
}
答案 2 :(得分:0)
你需要纠正你的for循环(用指针移动)
for(i = 0 ; i < 5 ; ++i)
{
sprintf(&b[i], "%d", a[i]);
}
答案 3 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
int main(){
int a[5] ={1, 2, 3, 4, 5};
int i;
char *b;
int n = sizeof(a)/sizeof(*a);
size_t len = 0;
for(i=0 ; i<n ; ++i){
len += snprintf(NULL, 0, "%d", a[i]);
}
b = malloc(len + 1);
len = 0;
for(i=0 ; i<n ; ++i){
len += sprintf(b + len, "%d", a[i]);
}
printf("\n%s\n", b);
free(b);
return 0;
}
答案 4 :(得分:0)
请注意以下代码:
#include <stdio.h>
int main()
{
int i, a[5] ={1,2,3,4,5};
char buff[sizeof(a)/sizeof(a[0]) + 1] = {0};
char *b = buff;
for(i=0 ; i<sizeof(a)/sizeof(a[0]) ; i++)
{
sprintf(b++,"%d",a[i]);
}
printf("%s\n", buff);
return 0;
}
您的代码需要确保执行以下操作:
- 创建一个存储您的数字字符串的位置。这样做是这样的:
char buff[sizeof(a)/sizeof(a[0]) + 1] = {0};
注意一些事情。
一个。您可以通过将数组的总大小除以其中一个元素的大小来计算a
数组中的整数数。您需要确保缓冲区在末尾有空间用于NULL,因此请添加1。
湾您想初始化缓冲区值。通过将此缓冲区分配给{0},您将得到一个缓冲区,其中所有元素都设置为0(NULL)。这保证了我们的缓冲区在最后需要NULL,因此printf()知道要打印多少字节。尽管sprintf()在字符串的末尾添加了一个NULL,但是养成正确初始化缓冲区和变量的习惯是个好主意。
- 您需要确保b
指向您的缓冲区。你这样做:
char *b = buff;
因为buff是一个数组buff
是第一个元素的地址。
- 最后在你的for循环中,它再次计算你正在迭代的数组中的元素数量,以便你不必记住改变循环如果你改变int数组的大小。