MATLAB: Basic question about pointer in mex function

mex

Hello,
I am getting confused with the basic concept of pointer in mex function. So I have couple of questions…
Below is an example from mathwork.
Q1.
As far as I know, in C++, when we declare double *y for the first time, y is the variable that stores address for data type double.
void arrayProduct(double x, double *y, double *z, mwSize n)
z[i] = x * y[i];
But I was wondering if z, y stores addresses not values how can we calculate like this z[i] = x * y[i]; ?
Shouldn't this be something like (*z)[i] and (*y)[i]? but then I see the message
error: subscripted value is neither array nor pointer nor vector
(*z)[i] = x * (*y)[i];
So I guess y stores the value.
Q2.
In the gateway function,
double *inMatrix; inMatrix is supposed to be a variable that stores addresses of data type double
inMatrix = mxGetPr(prhs[1]); inMatrix stores addresses of second argument of the input variable
Am I understanding this concept correctly?
Q3.
Then, 'inMatrix' is a pointer to 'A'
`y' is a pointer to 'inMatrix'
'multiplier' is a copy of 's'
'x' is a copy of 'multiplier'
Any help would be appreciated.
Thanks in advance.
#include "mex.h"
/* 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<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 */
/* 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]);
/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(1,(mwSize)ncols,mxREAL);
/* get a pointer to the real data in the output matrix */
outMatrix = mxGetPr(plhs[0]);
/* call the computational routine */
arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);
}
clc
clear all;
clear mex;
mex arrayProduct.c
s = 5;
A = [1.5, 2, 9];
B = arrayProduct(s,A)

Best Answer

Q1. But I was wondering if z, y stores addresses not values how can we calculate like this z[i] = x * y[i]; ?
This is a question about C, not Matlab.
The [ ] operator in C means accessing the contents the pointer points to. So this is equivalent:
z[i]
*(z + i)
The first notation is just a nicer abbreviation.
Q2. inMatrix = mxGetPr(prhs[1]); inMatrix stores addresses of second argument of the input variable
Almost correct. mxGetPr replies the address of the data section of this Matlab variable. The address of the variable itself is the pointer in prhs[1].
Matlab variables contain a header, in which e.g. the dimensions of the data are stored, and a pointer to an array, which contains the actual values.
Q3. I do not understand, what is the question here. I see some correct statements.