Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Created May 23, 2016 20:57
Show Gist options
  • Select an option

  • Save mlabbe/1af0e895ec06d37d14e37323bfdf7abd to your computer and use it in GitHub Desktop.

Select an option

Save mlabbe/1af0e895ec06d37d14e37323bfdf7abd to your computer and use it in GitHub Desktop.
ftg strcatall
/* append num strings, returning heap-allocated string.
caller must free() returned ptr. */
char *
ftg_strcatall(size_t num, ...)
{
size_t i, alloc_size = 0;
va_list va1, va2;
char *buf, *p_buf;
va_start(va1, num);
for (i = 0; i < num; i++)
{
alloc_size += strlen(va_arg(va1, char*));
}
va_end(va1);
alloc_size += 1;
buf = p_buf = (char*)ftg_malloc(sizeof(char)*alloc_size);
if (!buf) return NULL;
va_start(va2, num);
for (i = 0; i < num; i++)
{
char *s = va_arg(va2, char*);
while (*s)
{
*p_buf = *s;
p_buf++, s++;
}
}
va_end(va2);
buf[alloc_size-1] = '\0';
return buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment