vasprintf()

Updated: October 26, 2022

Write formatted output into an allocated string (varargs)

Synopsis:

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

int vasprintf( char **strp,
               const char *format,
               va_list arg );

Arguments:

strp
A pointer to a location where the function can store a pointer to the allocated string.
format
A string that specifies the format of the output. The formatting string determines what additional arguments you need to provide. For more information, see printf().
arg
A variable-argument list of the additional arguments, which you must have initialized with the va_start() macro.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The vasprintf() function formats data under control of the format control string. It's a “varargs” version of asprintf() and is similar to sprintf(), except that vasprintf() allocates a string for the formatted output. A null character is placed at the end of the generated character string. If vasprintf() is successful, it sets *strp to point to the string.

You should call free() to free the string when you're finished with it.

Returns:

The number of characters written into the string, not including the terminating null character, or a negative value if an output error occurred (errno is set, and *strp is left unchanged). An error can occur while converting a value for output.

Classification:

Unix

Safety:  
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes