Design Pattern Examples
Overview of object-oriented design patterns
formatstring.c
Go to the documentation of this file.
1
5
6#include <stdarg.h>
7#include <stdio.h>
8#include <stdlib.h>
9
10#include "formatstring.h"
11
13// formatstring()
15char* formatstring(const char* format, ...)
16{
17 va_list args;
18 va_start(args, format);
19 va_list args2;
20 va_copy(args2, args);
21 int numCharsNeeded = 0;
22 char *buffer = NULL;
23 if (format != NULL)
24 {
25 numCharsNeeded = vsnprintf(NULL, 0, format, args) + 1;
26 buffer = calloc(numCharsNeeded, sizeof(char));
27 if (buffer != NULL)
28 {
29 vsnprintf(buffer, numCharsNeeded, format, args2);
30 }
31 }
32 else
33 {
34 buffer = calloc(1, sizeof(char));
35 }
36 va_end(args2);
37 va_end(args);
38
39 return buffer;
40}
char * formatstring(const char *format,...)
Use the given string and arguments to return a buffer containing the formatted string....
Definition: formatstring.c:15
Declaration of the formatstring() function that replaces sprintf() and snprintf() by allocating the r...