GNU Info

Info Node: (libc.info)Dynamic Output

(libc.info)Dynamic Output


Next: Variable Arguments Output Prev: Formatted Output Functions Up: Formatted Output
Enter node , (file) or (file)node

Dynamically Allocating Formatted Output
---------------------------------------

   The functions in this section do formatted output and place the
results in dynamically allocated memory.

 - Function: int asprintf (char **PTR, const char *TEMPLATE, ...)
     This function is similar to `sprintf', except that it dynamically
     allocates a string (as with `malloc'; Note: Unconstrained
     Allocation) to hold the output, instead of putting the output in
     a buffer you allocate in advance.  The PTR argument should be the
     address of a `char *' object, and `asprintf' stores a pointer to
     the newly allocated string at that location.

     The return value is the number of characters allocated for the
     buffer, or less than zero if an error occurred. Usually this means
     that the buffer could not be allocated.

     Here is how to use `asprintf' to get the same result as the
     `snprintf' example, but more easily:

          /* Construct a message describing the value of a variable
             whose name is NAME and whose value is VALUE. */
          char *
          make_message (char *name, char *value)
          {
            char *result;
            if (asprintf (&result, "value of %s is %s", name, value) < 0)
              return NULL;
            return result;
          }

 - Function: int obstack_printf (struct obstack *OBSTACK, const char
          *TEMPLATE, ...)
     This function is similar to `asprintf', except that it uses the
     obstack OBSTACK to allocate the space.  Note: Obstacks.

     The characters are written onto the end of the current object.  To
     get at them, you must finish the object with `obstack_finish'
     (Note: Growing Objects).


automatically generated by info2www version 1.2.2.9