问题
I have an integer array:
int a[5]={5,21,456,1,3}
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?
回答1:
sprintf
do what you need.
Little example
char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
index += sprintf(&str[index], "%d", a[i]);
snprintf
takes care of the str length
char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
index += snprintf(&str[index], 128-index, "%d", a[i]);
回答2:
What you need to do is to convert the integer values in a
into character values in s
leaving room for a null-terminating character. In your case a total of 9 characters. sprintf
is the proper tool since vales exceed single digits:
#include <stdio.h>
int main(void)
{
int a[5]={5,21,456,1,3};
char s[9] = {0};
int n = 0;
for (int i = 0; i < 5; i++) {
n += sprintf (&s[n], "%d", a[i]);
}
printf ("\n char* s = %s\n\n", s);
return 0;
}
Output
$ ./bin/sprintf
char* s = 52145613
回答3:
Tried to go for a more readable approach even though the other answers are more efficient in terms of memory and instructions.
Tested it here.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void get_me_a_string(int * int_array, int array_size, char * output_string, int output_string_max_size)
{
if(!int_array || !output_string)
return;
char * aux_string = NULL;
//Depending on the compiler int is 2-byte or 4 byte.
//Meaning INT_MAX will be at most 2147483647 (10 characters + 1 '\0').
aux_string = (char *) malloc(11);
if(!aux_string)
return;
int i;
int current_array_size = 0;
for(i = 0; i < array_size; i++)
{
sprintf(aux_string, "%d", int_array[i]);
current_array_size += strlen(aux_string);
if(current_array_size < output_string_max_size)
strcat(output_string, aux_string);
else
break;
}
free(aux_string);
}
int main(void) {
int a[5]={5,21,456,1,3};
int string_max_size = 256;
char * string_from_array = NULL;
string_from_array = (char *) malloc(string_max_size);
if(NULL == string_from_array)
{
printf("Memory allocation failed. Exiting...");
return 1;
}
memset(string_from_array, 0, string_max_size);
get_me_a_string(a, 5, string_from_array, string_max_size);
printf(string_from_array);
free(string_from_array);
return 0;
}
回答4:
You can search for sprintf()/ snprintf() which can get the job done for you.
From the man page,
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
The functions in the
printf()
family produce output according to a format as described below....[...]...
sprintf()
,snprintf()
,vsprintf()
andvsnprintf()
write to the character stringstr
.
回答5:
Here is a demonstrative program that shows how this can be done.
#include <stdio.h>
int main(void)
{
int a[] = { 5, 21, 456, 1, 3 };
const size_t N = sizeof( a ) / sizeof( *a );
char s[10];
int n;
for ( size_t i = 0, j = 0;
i < N && ( n = snprintf( s + j, sizeof( s ) - j, "%d", a[i] ) ) > 0;
i++ )
{
j += n;
}
puts( s );
return 0;
}
The program output is
52145613
来源:https://stackoverflow.com/questions/30234363/how-can-i-store-an-int-array-into-string