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

Discussion in 'C Programming' started by henrytcy@gmail.com, Dec 7, 2005.

  1. Guest

    Hi,

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

    Thanks!
    , Dec 7, 2005
    #1
    1. Advertising

  2. Richard Bos Guest

    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
    Richard Bos, Dec 7, 2005
    #2
    1. Advertising

  3. Guest

    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, 2005
    #3
  4. Saif Guest

    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.
    Saif, Dec 7, 2005
    #4
  5. "Saif" <> writes:

    > 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;
    }
    Niklas Norrthon, Dec 7, 2005
    #5
  6. Guest

    why couldn't you just use asprintf ?

    Saif wrote:
    > 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, 2005
    #6
  7. Re: How to convert integer to string array without specify arraysize?

    writes:
    > why couldn't you just use asprintf ?


    There is no such thing as asprintf in C.

    DES
    --
    Dag-Erling Smørgrav -
    =?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=, Dec 7, 2005
    #7
  8. Saif Guest

    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.
    Saif, Dec 8, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. les ander
    Replies:
    4
    Views:
    857
    wes weston
    Oct 5, 2004
  2. Suresh V
    Replies:
    5
    Views:
    3,661
    SaticCaster
    Jul 5, 2010
  3. news
    Replies:
    2
    Views:
    1,095
  4. Randy Kramer
    Replies:
    12
    Views:
    348
    Robert Klemme
    Oct 25, 2007
  5. nick
    Replies:
    1
    Views:
    432
    David Mark
    Feb 13, 2011
Loading...

Share This Page