title: Returning a array from a void * argument in a function
body: <p>I have a variadic function that takes at least 3 arguments, and is of the form</p>
  <pre><code>foo(void* ret_val, int mode, int num_params, ...)
  </code></pre>
  <p>The general use of the variadic function <code>foo()</code> can involve returning different data types as well as an arbitrary number of input arguments that can have various data types, but I am unable to find a way of returning an array.</p>
  <p>How can I return an array?</p>
  <p>The function definition is that the first argument returns an arbitrary type depending on the function invoked by <code>foo()</code>, <code>mode</code> is the mode of operation, and <code>num_params</code> is the number of additional input parameters, which can be zero. There is a <code>switch</code> statement inside <code>foo()</code> which depending on <code>mode</code>, can invoke one or more functions, and can return a value of an arbitrary type.</p>
  <p>As a simplified example, suppose I have the following 3 functions that are invoked by <code>foo()</code>:</p>
  <pre><code>void bar1(int i, int, j) {
  // Do some calculations but return nothing.
  }
  
  int bar2() {
  // Do some calculations and return a value.
  }
  
  int* bar(3) {
  // Do some calculations with an array and return it.
  }
  </code></pre>
  <p>Then in <code>foo()</code> I can have the following code:</p>
  <pre><code>foo(void* ret_val, int mode, int num_params, ...) {
    va_list args;
    va_start(args, num_params);
    switch (mode) {
    case 1: {
            int a = va_arg(args, int);
            int b = va_arg(args, int);
            bar1(a, b);
            }
            break;
    case 2: {
            int val = bar1();
            *(int*)ret_val = val;
            }
            break;
    case 3: {
            int* p = new int[10];
            p = bar3();
            // Add a loop to test out that the array has been read from bar3(), which works.
            *(int*)ret_val = *p; // &lt;== This is where the problem is.
            }
            break;
        }
        va_end(args);
  }
          
  </code></pre>
  <p>Then in the main program I can invoke <code>foo()</code> in the following way:</p>
  <pre><code>int x;
  foo(&amp;x, 1, 2, 10, 20); // Call bar1()
  
  foo(&amp;x, 2, 0);         // Call bar2()
  printf(&quot;%d\n&quot;, x);
  
  int xd[10];            // Declare and initialize xd.
  for (int i = 0; i &lt; 10; i++) {
      xd[i] = 0;
  }
  foo(xd, 3, 0)          // Call bar3()
  for (int i = 0; i &lt; 10; i++) {
      printf(&quot;%d %d\n&quot;, i, xd[i]);
  }
  </code></pre>
  <p>I put in the calls for <code>bar1()</code> and <code>bar2()</code> for context, and they work. For <code>bar1()</code> two ints are provided, but nothing is returned, and a dummy output argument has to be provided. For <code>bar2()</code> no input values are given, but an output int is returned, which is cast to star(int star) which works and is returned correctly.</p>
  <p>However, for <code>bar3()</code> internally I generate a 10 element array as a test, which is correctly returned inside <code>foo()</code> from the print statement, but I am unable to return the array to <code>main()</code>. I have fiddled around with various statements involving pointers, but either a list of zeros is returned if the array is initialized, or garbage if it is not.</p>
  <p>How can I return an array correctly?</p>