title: variable arguments in c++ with different datatypes
body: <p>please read through the following code</p>
  
  <pre><code>#define  INT    0
  #define  CHAR   1
  #define  FLOAT  2
  #define  DOUBLE 3
  
  int createKey(int keySize,vector&lt;int&gt; &amp;types, ...){ 
      va_list varlist;
      va_start(varlist,types.size());
  
      int intKey;
      float floatKey;
      char charKey;
      double doubleKey;
  
      char *key = (char*)malloc(keySize);
      int offset = 0;
  
      for(int i=0;i&lt;types.size();i++){
          switch(types[i]){
              case INT:
                  intKey = va_arg(varlist,int);
                  memcpy(&amp;key[offset],&amp;intKey,sizeof(int));
                  offset += sizeof(int);
              break;
  
              case CHAR:
                  charKey = va_arg(varlist,char);
                  memcpy(&amp;key[offset],&amp;charKey,sizeof(char));
                  offset += sizeof(char);
              break;
  
              case FLOAT:
                  floatKey = va_arg(varlist,float);
                  memcpy(&amp;key[offset],&amp;floatKey,sizeof(float));
                  offset += sizeof(float);
              break;
  
              case DOUBLE:
                  doubleKey = va_arg(varlist,double);
                  memcpy(&amp;key[offset],&amp;doubleKey,sizeof(double));
                  offset += sizeof(double);
              break;
  
          }
          va_end(varlist);
      }
  
      int testIntKey;
      char testCharKey;
      float testFloatKey;
      double testDoubleKey;
  
      offset = 0;
  
      for(int i=0;i&lt;types.size();i++) {
          switch(types[i]){
              case INT:
                  memcpy(&amp;testIntKey,&amp;key[offset],sizeof(int));
                  cout&lt;&lt;testIntKey&lt;&lt;endl;
                  offset += sizeof(int);
              break;
  
              case CHAR:
                  memcpy(&amp;testCharKey,&amp;key[offset],sizeof(char));
                  cout&lt;&lt;testCharKey&lt;&lt;endl;
                  offset += sizeof(char);
  
              break;
  
              case FLOAT:
                  memcpy(&amp;testFloatKey,&amp;key[offset],sizeof(float));
                  cout&lt;&lt;testFloatKey&lt;&lt;endl;
                  offset += sizeof(float);
              break;
  
              case DOUBLE:
                  memcpy(&amp;testDoubleKey,&amp;key[offset],sizeof(double));
                  cout&lt;&lt;testDoubleKey&lt;&lt;endl;
                  offset += sizeof(double);
              break;
          }
      }
  
  }
  </code></pre>
  
  <p>In the above code, I am trying to create a key that is a combination of one or more of the datatypes(int,char,float,double)...I used ellipses as i donot know the number of arguments that may be passed to createKey(). Now the above code when compiled shows the following warnings..</p>
  
  <pre><code>varargsTest.cpp: In function ‘int createKey(int, std::vector&lt;int, std::allocator&lt;int&gt; &gt;&amp;, ...)’:
  varargsTest.cpp:20: warning: second parameter of ‘va_start’ not last named argument
  varargsTest.cpp:39: warning: ‘char’ is promoted to ‘int’ when passed through ‘...’
  varargsTest.cpp:39: note: (so you should pass ‘int’ not ‘char’ to ‘va_arg’)
  varargsTest.cpp:39: note: if this code is reached, the program will abort
  varargsTest.cpp:45: warning: ‘float’ is promoted to ‘double’ when passed through ‘...’
  varargsTest.cpp:45: note: if this code is reached, the program will abort
  </code></pre>
  
  <p>and when i run the program with the following ..</p>
  
  <pre><code>int main()
  {
      vector&lt;int&gt; types;
  
      types.push_back(INT);
      types.push_back(CHAR);
      types.push_back(INT);
  
      createKey(9,types,85,'s',97);
  
  }
  </code></pre>
  
  <p>I get <code>Illegal instruction.</code> </p>
  
  <p>How could this problem be solved...Is this the right approach to handle these kind of problems?</p>