title: R: how to let .C call update output variable?
body: <p>I am trying to write a C code to use in R, but found that the .C function call won't update output variable. The real C code is complicated, but here is a simple example to show the behavior:</p>
  
  <pre><code>void doubleMe(const int *input, int *output) {
      output[0] = input[0] * 2;
  }
  </code></pre>
  
  <p>Save above C function into file doubleMe.c. Under Linux, compile it to create doubleMe.so file:</p>
  
  <pre><code>R CMD SHLIB doubleMe.c
  </code></pre>
  
  <p>In R, if I do following:</p>
  
  <pre><code>dyn.load("doubleMe.so") # load it
  input = 2
  output = 0
  .C("doubleMe", as.integer(input), as.integer(output)) # expect output=4
  
  [[1]]
  [1] 2
  
  [[2]]
  [1] 4
  </code></pre>
  
  <p>The screen output indicates the input is doubled, but the output in R is still 0:</p>
  
  <pre><code>output
  [1] 0
  </code></pre>
  
  <p>If I do fowllwing</p>
  
  <pre><code>output = .C("doubleMe", as.integer(input), as.integer(output))[[2]]
  </code></pre>
  
  <p>the output is 4. This should work for the example.</p>
  
  <p>But my real input and output are matrix, and I have to reshape the output to correct dimension. Is there a way to let .C call update output directly?</p>