In a function like printf, we use stdarg.h
to handle the variadic parameters.
void print(int args,...){
va_list ap;
va_start(ap, args);
int i = 0;
for(i=0; i<args; i++){
printf("%d\n",va_arg(ap, int));
}
va_end(ap);
}
We want to parse the format list (the first argument given to our variadic function) to track the types of the arguments specified in the format list then, call va_arg with the appropriate type.
I make a first loop to parse the format list, store the specifiers letters into an array. So I know which type we expect and how many there is.
ex: ft_like_printf("Watch your %d %s\n", 6, "Spider pig");
specifiers_list = "ds"
So d <=> int and s <=> char* (same specifiers as printf)
But how to code it dynamically? What is the syntax to call va_arg with differents types ?
I have read THIS and THAT which I think are what I'm looking for, aren't they ? If yes, what to do with it ? What are the real case scenarios of a struc containing an enum + union or struct containing an union + function pointer ?
To handle the different data types, I had started with this:
typedef struct s_flist
{
char c;
(*f)();
} t_flist;
t_flist flist[] =
{
{ 's', &putstr },
{ 'i', &put_number },
{ 'd', &put_number }
};