need of a sprintf like function...

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

    #1

    need of a sprintf like function...

    Hello All,

    To use sprintf function we have to first create a char * and assign
    some memory to it or we have to fixed memory sized array.

    eg.
    char str[512];//or it can be
    //char * str =(char *)malloc(512);
    int d=100;
    float f=2.22;
    sprintf(str,"Te st of sprintf %d %f"),d,f);
    free(str);
    if there any library function which can avoid allocating memory to str.
    i.e i can write
    char * str = NULL;
    and directly pass str to that printf like function and the function
    will internally handle the allocation of memory and releasing it.

    so i can write the code like
    eg.
    char * str = NULL;
    testprintf(str, "Test of sprintf %d%f"),d,f);

    Is there any such library function available?

    Regards,
    Yogesh Joshi

  • Simon Biber

    #2
    Re: need of a sprintf like function...

    ypjofficial@ind iatimes.com wrote:[color=blue]
    > Hello All,
    >
    > To use sprintf function we have to first create a char * and assign
    > some memory to it or we have to fixed memory sized array.
    >
    > eg.
    > char str[512];//or it can be
    > //char * str =(char *)malloc(512);[/color]

    Don't cast the result of malloc. Make sure you do #include <stdlib.h>
    [color=blue]
    > int d=100;
    > float f=2.22;
    > sprintf(str,"Te st of sprintf %d %f"),d,f);
    > free(str);
    > if there any library function which can avoid allocating memory to str.
    > i.e i can write
    > char * str = NULL;
    > and directly pass str to that printf like function and the function
    > will internally handle the allocation of memory and releasing it.[/color]

    Sure, you can write a function like that. To do it properly, you'll need
    to use vsnprintf inside your function, but vsnprintf was added by C99
    and has not yet appeared in all implementations of the C standard library.
    [color=blue]
    > so i can write the code like
    > eg.
    > char * str = NULL;
    > testprintf(str, "Test of sprintf %d%f"),d,f);[/color]

    This exact form is not possible, since testprintf must modify the value
    of the pointer str, but it receives only a copy of the value, and has no
    way to return the new value to the caller. You need to pass a pointer to
    str.
    testprintf(&str ,"Test of sprintf %d%f", d, f);

    The prototype of the function should be
    int testprintf(char **pstr, const char *format, ...);

    Here's a possible implementation:

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

    int testprintf(char **pstr, const char *format, ...)
    {
    va_list ap1, ap2;
    va_start(ap1, format);
    va_copy(ap2, ap1);
    int n = vsnprintf(NULL, 0, format, ap1);
    *pstr = malloc(n);
    if(*pstr)
    {
    vsnprintf(*pstr , n, format, ap2);
    }
    va_end(ap1);
    va_end(ap2);
    return n;
    }

    int main(void)
    {
    char *str;
    testprintf(&str , "test sprintf %d %f", 42, 42.0);
    printf("%s\n", str);
    free(str);
    return 0;
    }

    --
    Simon.

    Comment

    • Thad Smith

      #3
      Re: need of a sprintf like function...

      ypjofficial@ind iatimes.com wrote:
      [color=blue]
      > if there any library function which can avoid allocating memory to str.
      > i.e i can write
      > char * str = NULL;
      > and directly pass str to that printf like function and the function
      > will internally handle the allocation of memory and releasing it.[/color]

      Yes. The C99 function snprintf has a length parameter, which you may
      set to 0.

      --
      Thad

      Comment

      • Simon Biber

        #4
        Re: need of a sprintf like function...

        Thad Smith wrote:[color=blue]
        > ypjofficial@ind iatimes.com wrote:
        >[color=green]
        >> if there any library function which can avoid allocating memory to str.
        >> i.e i can write
        >> char * str = NULL;
        >> and directly pass str to that printf like function and the function
        >> will internally handle the allocation of memory and releasing it.[/color]
        >
        >
        > Yes. The C99 function snprintf has a length parameter, which you may
        > set to 0.[/color]

        Setting the length parameter of snprintf to zero does not actually
        accomplish the task of allocating memory. It just allows you determine
        what length the string should have. You can then allocate a block of
        memory of the correct length, and call snprintf again to actually
        perform the conversion to string. See my other post in this thread.

        --
        Simon.

        Comment

        • Mike Wahler

          #5
          Re: need of a sprintf like function...


          <ypjofficial@in diatimes.com> wrote in message
          news:1130766753 .432421.308590@ z14g2000cwz.goo glegroups.com.. .[color=blue]
          > Hello All,
          >
          > To use sprintf function we have to first create a char * and assign
          > some memory to it or we have to fixed memory sized array.
          >
          > eg.
          > char str[512];//or it can be
          > //char * str =(char *)malloc(512);
          > int d=100;
          > float f=2.22;
          > sprintf(str,"Te st of sprintf %d %f"),d,f);
          > free(str);
          > if there any library function which can avoid allocating memory to str.
          > i.e i can write
          > char * str = NULL;
          > and directly pass str to that printf like function and the function
          > will internally handle the allocation of memory and releasing it.[/color]

          Well, if it did that, when it returned, whatever
          it wrote to the allocated memory would be gone.
          I don't see how that would be useful.

          -Mike


          Comment

          Working...