title: Determine types from a variadic function's arguments in C
body: <p>I'd like a step by step explanation on how to parse the arguments of a variadic function
  so that when calling <code>va_arg(ap, TYPE);</code> I pass the correct data TYPE of the argument being passed.</p>
  <p>Currently I'm trying to code <code>printf</code>.
  I am only <strong>looking for an explanation</strong> preferably <strong>with simple examples</strong> but <strong>not the solution</strong> to <code>printf</code> since I want to solve it myself.</p>
  <p>Here are three examples which look like what I am looking for:</p>
  <ol>
  <li><a href="https://stackoverflow.com/a/1689228/3206885">https://stackoverflow.com/a/1689228/3206885</a></li>
  <li><a href="https://stackoverflow.com/a/5551632/3206885">https://stackoverflow.com/a/5551632/3206885</a></li>
  <li><a href="https://stackoverflow.com/a/1722238/3206885">https://stackoverflow.com/a/1722238/3206885</a></li>
  </ol>
  <p>I know the basics of what <code>typedef</code>, <code>struct</code>, <code>enum</code> and <code>union</code> do but can't figure out some practical application cases like the examples in the links.</p>
  <p>What do they really mean? I can't wrap my brain around how they work.
  How can I pass the data type from a union to <code>va_arg</code> like in the links examples? How does it match?
  with a modifier like <code>%d</code>, <code>%i</code> ... or the data type of a parameter?</p>
  <p>Here's what I've got so far:</p>
  <pre><code>#include &lt;stdarg.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &quot;my.h&quot;
  
  typedef struct s_flist
  {
      char c;
      (*f)();
  }              t_flist;
  
  int     my_printf(char *format, ...)
  {
      va_list ap;
      int     i;
      int     j;
      int     result;
      int     arg_count;
      char    *cur_arg = format;
      char    *types;
      t_flist flist[] = 
      {
          { 's',  &amp;my_putstr  },
          { 'i',  &amp;my_put_nbr },
          { 'd',  &amp;my_put_nbr }
      };
  
      i = 0;
      result = 0;
      types = (char*)malloc( sizeof(*format) * (my_strlen(format) / 2 + 1) );
      fparser(types, format);
      arg_count = my_strlen(types);
  
      while (format[i])
      {
          if (format[i] == '%' &amp;&amp; format[i + 1])
          {
              i++;
              if (format[i] == '%')
                  result += my_putchar(format[i]);
              else
              {
                  j = 0;
                  va_start(ap, format);
                  while (flist[j].c)
                  {
                      if (format[i] == flist[j].c)
                          result += flist[i].f(va_arg(ap, flist[i].DATA_TYPE??));
                      j++;
                  }
              }
          }
          result += my_putchar(format[i]);
          i++;
      }
  
      va_end(ap);
      return (result);
  }
  
  char    *fparser(char *types, char *str)
  {
      int     i;
      int     j;
  
      i = 0;
      j = 0;
      while (str[i])
      {
          if (str[i] == '%' &amp;&amp; str[i + 1] &amp;&amp;
              str[i + 1] != '%' &amp;&amp; str[i + 1] != ' ')
          {
              i++;
              types[j] = str[i];
              j++;
          }
          i++;
      }
      types[j] = '\0';
      return (types);
  }
  </code></pre>