title: Why, when passing an array element to a function through a ParamArray, does a varpointer arrive in the function?
body: <p>I have noticed an oddity in VBA when using <code>ParamArray</code> and passing array elements through it. In some cases it is not the value of the array element that arrives in a function, but the var pointer. (Excel 2016, 32-bit).</p>
  
  <p>After some plodding I found that the definition of the function as a variant array - in combination with a parameter list combined with a <code>ParamArray</code> - seems to be where the unexpected behaviour originates, but I can't see any feasible reasons why this would be so.</p>
  
  <p>The behaviour reverts to normal when:<br>
  1) the variable r is removed in the function declarations<br>
  2) <code>b</code> is declared with <code>Dim b()</code><br>
  3) the function returns a <code>Variant</code>, rather than a <code>Variant()</code></p>
  
  <p>I appreciate that it's a rather esoteric question, and it appears to be controllable for in various ways, but is there an explanation out there that elucidates this behaviour?</p>
  
  <pre><code>Sub Variantarraybug()
     Dim b: b = [{1, 2, 3}]
  
     Debug.Print farray1(2, b(1))(0)
     Debug.Print Application.WorksheetFunction.Sum(farray1(2, b(1)))
     Debug.Print Join(farray1(2, b(1)), " ")
  
     Debug.Print farray2(2, b(1))(0)
     Debug.Print Application.WorksheetFunction.Sum(farray2(2, b(1)))
     Debug.Print Join(farray2(2, b(1)), " ")
     Debug.Print  VarPtr(b(1)), VarPtr(b(2))
  End Sub
  
  Function farray1(r, ParamArray plop()) As Variant
     farray1 = Array(plop(0), 3)
  End Function
  Function farray2(r, ParamArray plop()) As Variant()
     farray2 = Array(plop(0), 5)
  End Function
  </code></pre>
  
  <p>Result in the debug window:</p>
  
  <pre><code> 1  
   4  
  1 3  
   1  
   6  
  358808368 5  
   358808368     358808384 
  </code></pre>
  
  <p><strong>Note 1:</strong> My understanding is that the <code>VarPtr</code> function returns the memory location of the start address of the  memory required by that variable. Here it is used only to demonstrate that the unexpected number (<em>358808368</em>) that was seen by the <code>farray2</code> function, is in fact the address of that element. </p>
  
  <p><strong>Note 2:</strong> This happens independent of how you generate the array (e.g. <code>b=array(1,2,3)</code>, <code>b=[1,2,3]</code>, etc.) and how <code>b</code> is declared (<code>b</code>, <code>b(1 to 3)</code>, etc.). However, if you declare <code>b</code> with <code>Dim b()</code>, the unexpected behaviour disappears. (You can't print <code>VarPtr(b)</code> in that case, as <code>VarPtr</code> cannot accept array variables.)</p>