title: Changing the constant arguments in mex-function
body: <p>This question is part of a bigger project I'm working on. I have used a simpler mex-function to explain the issue I'm dealing with.</p>
  <p>The requirement is to change the arguments (variables on the RHS) passed to the mex-function. This is a necessary requirement.
  I have been able to change the variable in case of double * as argumjents. Here's the code:</p>
  <pre><code>#include &quot;mex.h&quot;
  /* The computational routine */
  void arrayProduct(double x, double *y, double *z, mwSize n)
      {
      mwSize i;
      /* multiply each element y by x */
      for (i=0; i&lt;n; i++) {
          z[i] = (x * y[i]);
      }
  }
  
  /* The gateway function */
  void mexFunction( int nlhs, mxArray *plhs[],
                int nrhs, const mxArray *prhs[])
  {
  double multiplier;              /* input scalar */
  double *inMatrix;               /* 1xN input matrix */
  size_t ncols;                   /* size of matrix */
  double *outMatrix;              /* output matrix */
  
  /* check for proper number of arguments */
  if(nrhs!=3) {
      mexErrMsgIdAndTxt(&quot;MyToolbox:arrayProduct:nrhs&quot;,&quot;Three inputs required.&quot;);
  }
  if(nlhs!=0) {
      mexErrMsgIdAndTxt(&quot;MyToolbox:arrayProduct:nlhs&quot;,&quot;Zero output required.&quot;);
  }
  
  /* get the value of the scalar input  */
  multiplier = mxGetScalar(prhs[0]);
  /* create a pointer to the real data in the input matrix  */
  inMatrix = mxGetPr(prhs[1]);
  /* get dimensions of the input matrix */
  ncols = mxGetN(prhs[1]);
  
  /* get a pointer to the real data in the output matrix */
  outMatrix = mxGetPr(prhs[2]);
  /* call the computational routine */
  arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);
  
  }
  </code></pre>
  <p>When I try to do the same thing using typecasting to int * it doesn't work.
  Here's the code that I have tried:</p>
  <h1>include &quot;mex.h&quot;</h1>
  <pre><code>/* The computational routine */
  void arrayProduct(double x, double *y, int *z, mwSize n)
  {
      mwSize i;
      /* multiply each element y by x */
      for (i=0; i&lt;n; i++) {
          z[i] = (x * y[i]);
      }
  }
  
  /* The gateway function */
  void mexFunction( int nlhs, mxArray *plhs[],
                    int nrhs, const mxArray *prhs[])
  {
      double multiplier;              /* input scalar */
      double *inMatrix;               /* 1xN input matrix */
      size_t ncols;                   /* size of matrix */
      int *outMatrix;              /* output matrix */
      /* check for proper number of arguments */
      if(nrhs!=3) {
          mexErrMsgIdAndTxt(&quot;MyToolbox:arrayProduct:nrhs&quot;,&quot;Two inputs required.&quot;);
      }
      if(nlhs!=0) {
          mexErrMsgIdAndTxt(&quot;MyToolbox:arrayProduct:nlhs&quot;,&quot;One output required.&quot;);
      }
  
      /* get the value of the scalar input  */
      multiplier = mxGetScalar(prhs[0]);
      int mult = (int)multiplier;
      /* create a pointer to the real data in the input matrix  */
      inMatrix = mxGetPr(prhs[1]);
     /* int *inMat;
      inMat = *inMatrix;*/
      /* get dimensions of the input matrix */
      ncols = mxGetN(prhs[1]);
      /* create the output matrix */
      /* get a pointer to the real data in the output matrix */
      outMatrix = (int *)mxGetData(prhs[2]);
      /* call the computational routine */
      arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);
  }
  </code></pre>
  <p>I need to convert double to int * in the case of my project and solving it on this simple example will solve the issue.
  Any suggestions?</p>