Re: int array to string?
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 04/01/04
- Next message: Sivarn: "Help with scanf"
- Previous message: Leor Zolman: "Re: int array to string?"
- In reply to: Amittai Aviram: "int array to string?"
- Next in thread: Amittai Aviram: "Re: int array to string?"
- Reply: Amittai Aviram: "Re: int array to string?"
- Reply: Alex: "Re: int array to string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 01 Apr 2004 03:46:28 GMT
"Amittai Aviram" <amittai@amittai.com> wrote in message
news:c4g1pc$2i5f4a$1@ID-124651.news.uni-berlin.de...
> Hi! Is there a convenient way to create a null-terminated string out of
an
> array of type int?
I'm not sure what that means, but I'll take a guess.
> Is there a standard library function I can use? If so,
> how? This seems elementary, but I can't seem to find the right thing --
the
> only thing I can think of is using sprintf repeatedly to push a copy of
the
> next integer from the array onto the growing string, moving the null
> terminator in the process out to the new end,
There's no need to 'move' the terminator, just write over it.
And the terminator indicating the 'new end' will be written
for you by 'sprintf()'.
>until all the integers have
> been converted into characters in the string (provided that I begin with
> adequate memory set aside for this task). But this seems clunky and
> inefficient. Thanks!
#include <stdio.h>
#include <string.h>
int main()
{
int array[] = {1, 2, 3, 4, 5};
char text[100] = {0};
char *p = text;
size_t i = 0;
for(i = 0; i < sizeof array / sizeof *array; ++i)
sprintf(p += strlen(p), "%d", array[i]);
printf("%s\n", text);
return 0;
}
Output:
12345
You may or may not find my code 'clunky and inefficient',
but that's the way it's done. If it doesn't do what you
want, please clarify.
-Mike
- Next message: Sivarn: "Help with scanf"
- Previous message: Leor Zolman: "Re: int array to string?"
- In reply to: Amittai Aviram: "int array to string?"
- Next in thread: Amittai Aviram: "Re: int array to string?"
- Reply: Amittai Aviram: "Re: int array to string?"
- Reply: Alex: "Re: int array to string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|