问题
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 }
};
回答1:
Types are not first-class citizens in C.
But there are not that many types you have to care about: you can safely cast from unsigned
to signed
and the other way around, same goes for char*
and void*
, so for a basic printf, you have to handle:
- char
- short
- int
- long
- float
- double
- void*
union
to the rescue !
typedef union
{
char as_char;
short as_short;
int as_int;
long as_long;
float as_float;
double as_double;
void* as_ptr;
} t_value;
typedef enum {
CHAR_T,
INT_T,
/* It goes on */
...
} t_type;
t_value get_value(va_list ap, t_type type) {
/* You can't avoid this step, there is no way to iterate over types */
switch (type) {
case T_CHAR:
return va_arg(ap, char);
case T_INT:
/* ... */
}
}
Then you just have to create a lookup table, storing both a function pointer and a t_type
for each valid format specifier.
来源:https://stackoverflow.com/questions/27483614/c-variadic-function-how-to-specify-which-type-to-give-to-va-arg