-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariadic_strjoin.c
47 lines (43 loc) · 1.6 KB
/
variadic_strjoin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* variadic_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phemsi-a <phemsi-a@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/17 21:43:27 by lcouto #+# #+# */
/* Updated: 2021/07/30 20:32:45 by phemsi-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** Note to the user: variadic function argument lists are NOT null terminated and
** there is no way to test if the list is over. Therefore, this function trusts
** the user to enter the correct quantity of arguments, otherwise it'll segfault.
*/
char *variadic_strjoin(unsigned int arg_quantity, ...)
{
va_list arg_list;
unsigned int i;
char *arg_buffer;
char *temp;
char *result;
va_start(arg_list, arg_quantity);
i = 0;
result = NULL;
while (i < arg_quantity)
{
arg_buffer = va_arg(arg_list, char *);
if (result == NULL)
result = ft_strdup(arg_buffer);
else
{
temp = ft_strjoin(result, arg_buffer);
free(result);
result = temp;
}
i++;
}
va_end(arg_list);
return (result);
}