title: Casting away the const-ness in an array of pointers to const, and other questions regarding C
body: <p>I have a bit of code I'm running to test multithreading in MATLAB mex functions (I know MATLAB isn't thread safe... I'm just playing around to see what happens).  The entry point to MATLAB C code functions has the signature of the mexFunction function in the code snipped at the bottom of the post.  Since I want to essentially pass the arguments from this function into another thread created using pthreads, I need to bundle everything up into a structure.  The signature of the mexFunction (which I cannot change) includes an array of pointers to mxArray, however I can't directly include an array of pointers to mxArray in the structure definition since then I couldn't assign to that field of the structure (can't assign to an array).  For example, this doesn't work:</p>
  
  <pre><code>typedef struct MexFunArgs {
    int nrhs;
    const mxArray *prhs[];
  } MexFunArgs;
  
  /* ... now within the mexFunction ... */
  
  MexFunArgs mfa;
  
  mfa.prhs = prhs;
  </code></pre>
  
  <p>A first thought to fix this is to change the structure definition to this:</p>
  
  <pre><code>typedef struct MexFunArgs {
    int nrhs;
    const mxArray **prhs;
  } MexFunArgs;
  </code></pre>
  
  <p>However, then when I use this structure in the other thread, I would need to cast back to an array of pointers in order to pass it to the mexCallMATLAB function, which as far as I know can't be done (please correct me if I'm wrong here!).</p>
  
  <p>So, I thought instead I would change the structure definition to this:</p>
  
  <pre><code>typedef struct MexFunArgs {
    int nrhs;
    const mxArray *(*prhs)[];
  } MexFunArgs;
  </code></pre>
  
  <p>I.e., a pointer to an array of pointers to mxArray.  This solves the assignment issue (though the compiler still complains about incompatible pointer types for reasons I don't understand).  However, it turns out the mexCallMATLAB function doesn't take an array of pointers to const mxArrays, but instead an array of pointers to non-const mxArrays.  So now I'd like to know if there's a way of casting away the const aspect of my pointer to an array of pointers to const mxArrays.  I have no idea how to do that... the following isn't legal:</p>
  
  <pre><code>(mxArray *(*)[])
  </code></pre>
  
  <p>Can it be done?  If not, is what I'm trying to do (pass these arguments to the other thread unscathed in such a way that they can be used without the compiler whining) possible in some other way in C?</p>
  
  <p>The following is the complete code I'm using at the moment, which runs as expected, but produces a warning when compiled.  I hate warnings, and want it to go away.  What is the right way of making that happen?</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;mex.h&gt;
  
  typedef struct MexFunArgs {
    int nrhs;
    mxArray *(*prhs)[];
  } MexFunArgs;
  
  void *do_thread(void *args) {
    MexFunArgs *mfa = (MexFunArgs*) args;
  
    mexCallMATLAB(0, NULL, mfa-&gt;nrhs, *mfa-&gt;prhs, "disp");
  
    pthread_exit(NULL);
  }
  
  void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    int num;
  
    if (nrhs &lt; 1)
      mexErrMsgTxt("not enough input arguments");
    else
      num = mxGetScalar(prhs[0]);
  
    MexFunArgs mfa;
  
    mfa.nrhs = nrhs;
    mfa.prhs = &amp;prhs[1]; /* &lt;-- threads.c:29: warning: assignment from incompatible pointer type */
  
    pthread_t threads[num];
  
    int rc, t;
    for (t = 0; t &lt; num; t++) {
      mexPrintf("In main: creating thread %d\n", t);
      rc = pthread_create(&amp;threads[t], NULL, do_thread, (void *) &amp;mfa);
      if (rc)
        mexErrMsgTxt("Problem with return code from pthread_create()");
    }
    return;
  }
  </code></pre>