Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      asprintf, aswprintf, vasprintf, vaswprintf

      From cppreference.com
      <c‎ |experimental‎ |dynamic
       
       
       
       
      Defined in header<stdio.h>
      int asprintf(char**restrict strp,constchar*restrict fmt, ...);
      (1)(dynamic memory TR)
      int aswprintf(wchar_t**restrict strp,constwchar_t*restrict fmt, ...);
      (2)(dynamic memory TR)
      int vasprintf(char**restrict strp,constchar*restrict fmt,
                     va_list arg);
      (3)(dynamic memory TR)
      int vaswprintf(wchar_t**restrict strp,constwchar_t*restrict fmt,
                      va_list arg);
      (4)(dynamic memory TR)
      1) Analog ofsprintf, except that it allocates a storage large enough to hold the output including the terminating null character, as if by a call tomalloc, and returns a pointer to that storage via the first argument. This pointer should be passed tofree to release the allocated storage when it is no longer needed.
      2) Same as(1), except that it works with wide characterswchar_t (by analogy withswprintf).
      3) Same as(1), with the variable argument list replaced byarg, which shall be initialized by theva_start macro (and possibly subsequentva_arg calls).
      4) Same as(3), except that it works with wide characterswchar_t.

      Contents

      [edit]Parameters

      strp - A pointer to achar* orwchar_t* which will contain the formatted output
      fmt - A format string as withprintf/wprintf and related functions
      arg - Any extra arguments are used as withvsprintf andvswprintf

      [edit]Return value

      The number of characters written, just likesprintf(1),swprintf(2),vsprintf(3), orvswprintf(4), respectively. If memory allocation wasn't possible, or some other error occurs, these functions will return-1, and the contents ofstrp is undefined.

      [edit]Notes

      These functions are GNU extensions, not in C or POSIX. They are also available under *BSD. The FreeBSD implementation setsstrp toNULL on error.

      Thevasprintf andvaswprintf functions do not invoke theva_end macro.

      [edit]Example

      Can be tested with clang (C11)

      Run this code
      #include <stdio.h>#include <stdlib.h>#include <stdarg.h> void test(constchar*fmt, ...){char* dyn_buf; printf("Demo asprintf:\n");constint written_1= asprintf(&dyn_buf,"%s", fmt);printf("dyn_buf:\"%s\"; %i chars were written\n", dyn_buf, written_1);free(dyn_buf); printf("Demo vasprintf:\n");    va_list args;    va_start(args, fmt);constint written_2= vasprintf(&dyn_buf, fmt, args);    va_end(args);printf("dyn_buf:\"%s\"; %i chars were written\n", dyn_buf, written_2);free(dyn_buf);} int main(void){    test("Testing... %d, %d, %d",1,2,3);}

      Output:

      Demo asprintf:dyn_buf: "Testing... %d, %d, %d"; 21 chars were writtenDemo vasprintf:dyn_buf: "Testing... 1, 2, 3"; 18 chars were written
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/experimental/dynamic/asprintf&oldid=161853"

      [8]ページ先頭

      ©2009-2026 Movatter.jp