Created
May 16, 2015 02:50
-
-
Save spedru/d2460411cab52580a733 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdarg.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int vasprintf(char **strp, const char *fmt, va_list ap) | |
{ | |
char buf; | |
va_list ap2; | |
va_copy(ap2, ap); | |
int len = vsnprintf(&buf, 1, fmt, ap) + 1; | |
return len < 1 || !(*strp = malloc(len)) | |
? (*strp = NULL), -1 | |
: vsprintf(*strp, fmt, ap2); | |
} | |
int asprintf(char **strp, const char *fmt, ...) | |
{ | |
va_list args; | |
va_start(args, fmt); | |
int ret = vasprintf(strp, fmt, args); | |
va_end(args); | |
return ret; | |
} | |
int main() | |
{ | |
char *benis; | |
asprintf(&benis, "%d %d %d", 1, 1, 1); | |
puts(benis); | |
free(benis); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment