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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • henrytcy@gmail.com

    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!

  • Richard Bos

    #2
    Re: How to convert integer to string array without specify array size?

    henrytcy@gmail. com wrote:
    [color=blue]
    > How can I convert integer, for example 12113, to char array but without
    > specify an array size in C programming?[/color]

    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

    Comment

    • henrytcy@gmail.com

      #3
      Re: How to convert integer to string array without specify array size?

      Hi Richard,

      Thank you for you reply!

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

      Thanks?

      Comment

      • Saif

        #4
        Re: How to convert integer to string array without specify array size?

        henrytcy@gmail. com wrote:[color=blue]
        > How can I convert integer, for example 12113, to char array but without
        > specify an array size in C programming?[/color]

        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(121 13)) + 1 = 4 + 1 = 5

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

        Comment

        • James.Mahler@gmail.com

          #5
          Re: How to convert integer to string array without specify array size?

          why couldn't you just use asprintf ?

          Saif wrote:[color=blue]
          > henrytcy@gmail. com wrote:[color=green]
          > > How can I convert integer, for example 12113, to char array but without
          > > specify an array size in C programming?[/color]
          >
          > 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(121 13)) + 1 = 4 + 1 = 5
          >
          > You can now allocate a string with size characters using malloc and use
          > it to store your converted string.[/color]

          Comment

          • Dag-Erling Smørgrav

            #6
            Re: How to convert integer to string array without specify arraysize?

            James.Mahler@gm ail.com writes:[color=blue]
            > why couldn't you just use asprintf ?[/color]

            There is no such thing as asprintf in C.

            DES
            --
            Dag-Erling Smørgrav - des@des.no

            Comment

            • Niklas Norrthon

              #7
              Re: How to convert integer to string array without specify array size?

              "Saif" <saifch@gmail.c om> writes:
              [color=blue]
              > henrytcy@gmail. com wrote:[color=green]
              > > How can I convert integer, for example 12113, to char array but without
              > > specify an array size in C programming?[/color]
              >
              > 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(121 13)) + 1 = 4 + 1 = 5[/color]

              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;
              }

              Comment

              • Saif

                #8
                Re: How to convert integer to string array without specify array size?

                Niklas Norrthon wrote:[color=blue]
                > If I'm going to convert it to an char array I'd use snprintf to
                > calculate the size:[/color]

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

                Comment

                Working...