Created
May 23, 2016 20:57
-
-
Save mlabbe/1af0e895ec06d37d14e37323bfdf7abd to your computer and use it in GitHub Desktop.
ftg strcatall
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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