title: Calling a .c file from MATLAB yields a segmentation violation
body: <p>I want to run a two C programs called "hello1.c" and "hello2.c" from matlab.</p>
  
  <p><strong>My <code>hello1.c</code> code is as follows:</strong></p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;mex.h&gt;
  #include &lt;math.h&gt;
  #include "matrix.h"
  
  
  void funcc(double **f, double **x, long n, long b, double lambda, double theta)
  {
      long i;
      long j;
      double u,v,y,z,w;
      y = theta*lambda;
      w = lambda*lambda;
      z = 0.5*(theta+1.0)*w;
  
      u = 0.0;
      for(i=0;i&lt;n;i++)
      {
          for(j=0;j&lt;b;j++)
          v = fabs(x[i][j]);
          if (v &lt;= lambda)
              u += lambda*v;
          else if (v &gt; y)
              u += z;
          else
              u += 0.5*(v*(2*y - v) - w)/(theta-1.0);
          }
      **f = u;
      return;
  }
  
  
  void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
  {
      /*set up input arguments */
      double **x       =            mxGetPr(prhs[0]);
      long     n      =      (long)mxGetScalar(prhs[1]);
      long     b      =      (long)mxGetScalar(prhs[2]);
      double  lambda  =            mxGetScalar(prhs[3]);
      double  theta   =            mxGetScalar(prhs[4]);
      int     type    =       (int)mxGetScalar(prhs[5]);
  
      double **f;
  
  
      /* set up output arguments */
      plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
  
      f=mxGetPr(plhs[0]);
  
      funcc(f, x, n, b, lambda, theta);
      }
  </code></pre>
  
  <p><strong>My <code>hello2.c</code> code is as follows:</strong></p>
  
  <pre><code>long mymin(double *x, long n)
  {
      long i, ind;
      double temp;
      temp = x[0];
      ind = 0;
      for (i=1;i&lt;n;i++)
      {
          if (x[i] &lt; temp)
          {
              ind = i;
              temp = x[i];
          }
  
      }
      return ind;
  }
  
  
          void funnn(double *x, double *d, long n, long b, double lambda, double theta)
          {
              long i;
              long j;
              double u,z,w;
              double xtemp[3],ytemp[3];
              z = theta*lambda;
              w = lambda*lambda;
              for(i=0;i&lt;n;i++)
              {
                  for(j=0;j&lt;b;j++)
                  u = fabs(d[j*n + i]);
                  xtemp[0] = min(lambda,max(0,u - lambda));
                  xtemp[1] = min(z,max(lambda,(u*(theta-1.0)-z)/(theta-2.0)));
                  xtemp[2] = max(z,u);
  
                  ytemp[0] = 0.5*(xtemp[0] - u)*(xtemp[0] - u) + lambda*xtemp[0];
                  ytemp[1] = 0.5*(xtemp[1] - u)*(xtemp[1] - u) + 0.5*(xtemp[1]*(-xtemp[1] + 2*z) - w)/(theta-1.0);
                  ytemp[2] = 0.5*(xtemp[2] - u)*(xtemp[2] - u) + 0.5*(theta+1.0)*w;
                  x[j*n + i] = xtemp[mymin(ytemp,3)];
                  x[j*n + i] = d[j*n + i]&gt;= 0 ? x[j*n + i] : -x[j*n + i];
              }
              //return;
          }
  
  
  
          void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
          {
              /*set up input arguments */
              double* d       =            mxGetPr(prhs[0]);
              long     n      =      (long)mxGetScalar(prhs[1]);
              long     b      =      (long)mxGetScalar(prhs[2]);
              double  lambda  =            mxGetScalar(prhs[3]);
              double  theta   =            mxGetScalar(prhs[4]);
  
              double *x;
  
  
              /* set up output arguments */
              plhs[0] = mxCreateDoubleMatrix(n,b,mxREAL);
  
              x=mxGetPr(plhs[0]);
  
              funnn(x, d, n, b, lambda, theta);
          }
  </code></pre>
  
  <p>The problem is that when I compile my MATLAB code in which it calls my C functions (of course I have already written <code>mex hello1.c</code> and <code>mex hello2.c</code>), MATLAB returns me the screen which exhibits that MATLAB has encountered an internal problem and needs to close. In fact this error was detected while a MEX-file was running.</p>
  
  <p>Is this because my functions <code>mexFunction</code> are not written correctly? Can anyone who knows how to write MEX functions help me? </p>
  
  <p>Any help will be very appreciated!</p>