Ellipsis ..., in the prototype of a function, is used to designate a function as variational. That is, it allows you to pass a variable number of arguments to a function. In this form, the function should determine some way for the user to specify exactly how many arguments they presented, since the variational library functions in C ++ cannot dynamically determine this information.
For example, the stdio function printfis one such function with a prototype:
int printf(const char *format, ...);
, information_log, , printf , , printf .
:
#include <cstdarg> // or (#include <stdarg.h>)
#include <cstdio>
#include <cstring>
void concatenate(char ** out, int num_str, ...)
{
va_list args;
va_start(args, num_str);
int out_len = 0;
int * lengths = new int[num_str];
char ** strings = new char*[num_str];
for(int i = 0; i < num_str; i++)
{
strings[i] = va_arg(args, char *);
lengths[i] = strlen(strings[i]);
out_len += lengths[i];
}
int dest_cursor = 0;
(*out) = new char[out_len + 1];
for(int i = 0; i < num_str; i++)
{
strncpy( (*out) + dest_cursor, strings[i], lengths[i]);
dest_cursor += lengths[i];
}
(*out)[dest_cursor] = '\0';
delete [] strings;
delete [] lengths;
va_end(args);
}
int main()
{
char * output = NULL;
concatenate(&output, 5, "The ", "quick", " brown ", "fox ", "jumps!\n");
printf("%s", output);
delete [] output;
return 0;
}