Skip to content

Instantly share code, notes, and snippets.

@schwern
Last active May 10, 2017 23:00
Show Gist options
  • Save schwern/ccaa8d4d2868a520592e49dabc5d310d to your computer and use it in GitHub Desktop.
Save schwern/ccaa8d4d2868a520592e49dabc5d310d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
char * strcat_ex( const char *s, ... )
{
if ( s == NULL ) {
return NULL;
}
va_list ap;
size_t n = 0;
va_start( ap, s );
for ( const char *p = s; p != NULL; p = va_arg( ap, const char * ) )
{
n += strlen( p );
}
va_end( ap );
++n;
char *result = malloc( n );
if ( result == NULL ) {
return NULL;
}
n = 0;
va_start( ap, s );
for ( const char *p = s; p != NULL; p = va_arg( ap, const char * ) )
{
strcpy( result + n, p );
n += strlen( p );
}
va_end( ap );
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment