Variadic function and va_arg : variabilizing the type to be printed
<p>I am trying to recode a <code>printf</code> function so I need to use <code>va_arg</code> since <code>printf</code> is a variadic function.</p>

<p>The thing is, when I want to print out the arguments of <code>my_printf</code>, they can be <code>%i</code> (<code>int</code>) or <code>%s</code> (<code>char *</code>) if we take these two basic possibilities.</p>

<p>So when I go through my <code>va_list ap</code>, I tried to put a variable in the <code>va_arg</code> function so I can change the type to be written, like this:</p>

<pre><code>char *str = NULL;
str = ft_set_my_types(flags[cur_arg]);          
ft_putstr(va_arg(ap, str));
</code></pre>

<p>with my function <code>ft_set_my_types</code> being something like:</p>

<pre><code>char *ft_set_my_types(char *flags)
{
    char **tab = NULL;

    tab = ft_alloc_mem2(2, 15); /* this function malloc a tab */
    char a = 65;

     /* the example here is only with two types of variables */
     /* I am sending flags %s in my example which correspond to "char *" */

    tab['s'] = "char *";
    tab['d'] = "int ";

    size_t len = ft_strlen(flags) - 1;
    while (a &lt; 123)
    {
        if (a == flags[len])
            break ;
        else
            a++;
    }
    return (tab[a]);
}
</code></pre>

<p>The "char *flags" being everything between the "%" and the flag (included) of <code>my_printf</code>.</p>

<p>The problem is: when I compile it does not understand my tab['s'] as <code>char *</code> but as "char *" so I get this error:</p>

<pre><code>ft_print.c:63:25: error: unknown type name 'str'
                        ft_putstr(va_arg(ap, str));
                                             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/include/stdarg.h:35:50: note:
      expanded from macro 'va_arg'
#define va_arg(ap, type)    __builtin_va_arg(ap, type)
1 error generated.
</code></pre>

<p>Could you help me to figure out how I can insert my variable <code>*str</code> in this <code>va_arg</code> function?</p>