title: Segfault when returning a value from C to R using .Call (incompatible pointer types returning)
body: <p>I am learning to use the C programming language and am having a problem probably of incompatible pointer types returning. </p>
  
  <p>I have a text file with two columns that is read by R as a data.frame. After reading this i want to perform a .Call for a function in C that will read that date.frame and will return the value for a variable in R, however a segfault error occurs when trying to return the value. I can not find the solution to this problem, can anyone help me?</p>
  
  <p>The text file is structured in two columns as in the example below.</p>
  
  <pre><code>Q0045   YJL166W
  Q0045   YDL085W
  Q0045   YDR119W-A
  </code></pre>
  
  <p>Here is the C code for reading the data.frame.</p>
  
  <pre><code>#include &lt;Rinternals.h&gt;
  #include &lt;Rdefines.h&gt;
  #include &lt;R.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  char **test(SEXP lst){
    int i,elLength;
    int len = length(lst);
    SEXP col1, col2;
    char ***target = malloc(sizeof(char **) *len);
    col1 = VECTOR_ELT(lst, 0);
    col2 = VECTOR_ELT(lst, 1);
    elLength = length(col1);
    target[0] = malloc(sizeof(char *) * elLength);
    target[1] = malloc(sizeof(char *) * elLength);
    for (i=0; i&lt;elLength; i++) {
      target[0][i] = CHAR(STRING_ELT(col1, i));
      target[1][i] = CHAR(STRING_ELT(col2, i));
  
    }
  
    return target;
  } 
  </code></pre>
  
  <p>After this i create the .so file with the command line in terminal:</p>
  
  <pre><code>R CMD SHLIB test.c
  </code></pre>
  
  <p>And finally the code in R that read the file and execute the .Call.</p>
  
  <pre><code>dyn.load("/home/lab/test.so")
  fileR = data.frame(read.table("file.txt", sep = "\t", stringsAsFactors = FALSE))
  fileFromC = .Call("test", fileR)
  </code></pre>
  
  <p>After that i got the error running in R from terminal:</p>
  
  <pre><code> *** caught segfault ***
  address 0x310000c0, cause 'memory not mapped'
  </code></pre>
  
  <p>If i only print and return R_NilValue the error don't show. But i need the return of C process to a new variable.</p>