#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
int main(int argc, char * argv[])
{
char *arr[] = { "ab", "cd", "ef" };
char **ptr, **p, *str;
int num = 3;
int size = 0;
ptr = calloc(num, 4);
p = ptr;
for (; num > 0; num--)
size += strlen(*(p++) = arr[num - 1]);
str = calloc(1, ++size);
sprintf(str, "%s%s%s", ptr[0], ptr[1], ptr[2]);
printf("%s\n", str);
return 0;
}
conclusion: "efcdab"as expected.
now all this is fine and suitable if the count to argument is sprintfpredefined and known. However, what I'm trying to achieve is an elegant way to build a string if the count argument is a variable ( ptr[any]).
first problem: the second argument to pass to sprintfis const char *format.
second: the third argument is the actual number of arguments passed to build the string based on the provided format.
how can i achieve any of the following:
sprintf(str, "...", ...)
, , 4 ( ) char , ( , , 3). , ( ) "%s%s%s%s", ptr[0], ptr[1], ptr[2], ptr[3].
"" , sprintf ( vsprintf), ? , (** ptr) .. ? , , sprintf , ... - format.
/?