title: How to handle passing/returning array pointers to emscripten compiled code?
body: <p>From a windows 7 environment, I've used emcc to compile a simple c program which accepts an array and modifies it (see below).</p>
  <pre><code>double* displayArray(double *doubleVector) {
  
     for (int cnt = 0; cnt &lt; 3; cnt++) 
         printf(&quot;doubleVector[%d] = %f\n&quot;, cnt, doubleVector[cnt]);
  
     doubleVector[0] = 98;
     doubleVector[1] = 99;
     doubleVector[2] = 100;
  
     for (int cnt1 = 0; cnt1 &lt; 3; cnt1++) 
         printf(&quot;modified doubleVector[%d] = %f\n&quot;, cnt1, doubleVector[cnt1]);
      
     return doubleVector;
  }
  
  int main() {
  
     double d1, d2, d3;
     double array1[3];
     double *array2;
  
     array1[0] = 1.00000;
     array1[1] = 2.000000;
     array1[2] = 3.000000;
  
     array2 = displayArray(array1);
  
     for (int cntr =0; cntr &lt; 3; cntr++)
         printf(&quot;array1[%d] = %f\n&quot;, cntr, array1[cntr]);
      
     for (int cnt = 0; cnt &lt; 3; cnt++)
         printf(&quot;array2[%d] = %f\n&quot;, cnt, array2[cnt]);
      
     return 1;
  }
  </code></pre>
  <p>Using the -o options for emcc, I generated a .html file which I loaded to a browser (Chrome).</p>
  <pre><code>python emcc displayArray7.c -o displayArray7.html -s EXPORTED_FUNCTIONS=&quot;['_main', '_displayArray'
  </code></pre>
  <p>Upon loading, I see that the output being generated within the browser window is as expected (see below).</p>
  <pre><code>doubleVector[0] = 1.000000
  doubleVector[1] = 2.000000
  doubleVector[2] = 3.000000
  modified doubleVector[0] = 98.000000
  modified doubleVector[1] = 99.000000
  modified doubleVector[2] = 100.000000
  array1[0] = 98.000000
  array1[1] = 99.000000
  array1[2] = 100.000000
  array2[0] = 98.000000
  array2[1] = 99.000000
  array2[2] = 100.000000
  </code></pre>
  <p>However, when using the module.cwrap() command via javascript console and attempting to invoke the function directly (outside of main()) ,</p>
  <pre><code>&gt; displayArray=Module.cwrap('displayArray', '[number]', ['[number]'])
  
  &gt; result = displayArray([1.0,2.0,3.0])
  [1, 2, 3]
  &gt; result
  [1, 2, 3]
  </code></pre>
  <p>I am seeing the following being generated/displayed in the browser which is NOT what I expect to see.</p>
  <pre><code>doubleVector[0] = 0.000000
  doubleVector[1] = 0.000000
  doubleVector[2] = 0.000000
  modified doubleVector[0] = 100.000000
  modified doubleVector[1] = 100.000000
  modified doubleVector[2] = 100.000000   
  </code></pre>
  <p>I have the following questions:</p>
  <ol>
  <li><p>Do I have the syntax correct for the return type and parameter listing correct in my call to Module.cwrap()?  I've successfully run the simple, straight-forward example of int_sqrt() in the &quot;Interacting with code&quot; section of the tutorial which deals with passing non-pointer variables to the int_sqrt() routine.</p>
  </li>
  <li><p>Is there something different that is happening when arrays and/or pointers are passed to  (or returned from) the emscripten-generated javascript code?</p>
  </li>
  <li><p>How is that the generated output in the browser of the function, displayArray(), works (as expected) when called from main(); but not via the javascript console?</p>
  </li>
  </ol>