472,119 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

How to convert integer to string array without specify array size?

Hi,

How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?

Thanks!

Dec 7 '05 #1
7 13735
he******@gmail.com wrote:
How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?


You can't. Where would you put it? In random, sizeless memory? There is
no such thing. Whenever you have an array, you will have had to specify
its size somehow.

Richard
Dec 7 '05 #2
Hi Richard,

Thank you for you reply!

But how can I read an integer for example, 12113, one by one for
comparison?

Thanks?

Dec 7 '05 #3
he******@gmail.com wrote:
How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?


You can use dynamically allocated memory. To get the size of the string
needed, you can find the log (base 10) of your number, truncate it, and
add 1.

For example for 12113...

size = floor(log10(12113)) + 1 = 4 + 1 = 5

You can now allocate a string with size characters using malloc and use
it to store your converted string.

Dec 7 '05 #4
why couldn't you just use asprintf ?

Saif wrote:
he******@gmail.com wrote:
How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?


You can use dynamically allocated memory. To get the size of the string
needed, you can find the log (base 10) of your number, truncate it, and
add 1.

For example for 12113...

size = floor(log10(12113)) + 1 = 4 + 1 = 5

You can now allocate a string with size characters using malloc and use
it to store your converted string.


Dec 7 '05 #5
Ja**********@gmail.com writes:
why couldn't you just use asprintf ?


There is no such thing as asprintf in C.

DES
--
Dag-Erling Smørgrav - de*@des.no
Dec 7 '05 #6
"Saif" <sa****@gmail.com> writes:
he******@gmail.com wrote:
How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?


You can use dynamically allocated memory. To get the size of the string
needed, you can find the log (base 10) of your number, truncate it, and
add 1.

For example for 12113...

size = floor(log10(12113)) + 1 = 4 + 1 = 5


If I'm going to convert it to an char array I'd use snprintf to
calculate the size:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char* buf = NULL;
int x = 12113;
size_t sz = snprintf(buf, 0, "%d", x) + 1;
buf = malloc(sz);
snprintf(buf, sz, "%d", x);
/* do something with buf */
free(buf);

return 0;
}
Dec 7 '05 #7
Niklas Norrthon wrote:
If I'm going to convert it to an char array I'd use snprintf to
calculate the size:


snprintf is a new function added in C99 and may not be supported by all
compilers. I know it's not supported in mine.

Dec 8 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mel WEaver | last post: by
4 posts views Thread by Bill Borg | last post: by
43 posts views Thread by Steven T. Hatton | last post: by
10 posts views Thread by sposes | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.