From: Jim Rockford on
Suppose I have an integer value defined through a struct variable in a
Matlab C mex file (C++ actually)


nsize = mystruct.n; // this is integer valued (const int) and has a
specific value upon initialization


I wish to use the variable "nsize" (which at this point has a specific
value) to set the size of an array of pointers of mxArray type:


mxArray *x[mystruct.n]; // doesn't work
mxArray *x[nsize]; // doesn't work either
mxArray *x[4]; // this works, of course


When I compile this I get an error. The compiler complains
"error C2057: expected constant expression"

I basically know what this error means, but I don't know how to get
around it. Any help is appreciated.

Thanks,
Jim
From: Malcolm McLean on
Jim Rockford <jim.rockford1(a)gmail.com> wrote in message
>
> mxArray *x[mystruct.n]; // doesn't work
> mxArray *x[nsize]; // doesn't work either
> mxArray *x[4]; // this works, of course
>
mxArray **x = mxMalloc(mystruct.n * sizeof(maArray *));

From: Jim Rockford on
On Jul 26, 1:25 pm, "Malcolm McLean" <malcolm.mcle...(a)btinternet.com>
wrote:
> JimRockford<jim.rockfo...(a)gmail.com> wrote in message
>
> > mxArray *x[mystruct.n];     // doesn't work
> > mxArray *x[nsize];            // doesn't work either
> > mxArray *x[4];                 // this works, of course
>
> mxArray **x = mxMalloc(mystruct.n * sizeof(maArray *));


Thanks for the reply (though I think you meant "mxArray" instead of
"maArray"). However, what you propose does not work. I get the
compilation error

error C2440: 'initializing' : cannot convert from 'void *' to 'mxArray
**'
Conversion from 'void*' to pointer to non-'void' requires an
explicit cast
From: James Tursa on
Jim Rockford <jim.rockford1(a)gmail.com> wrote in message <fc540812-ab06-42b8-86c8-03809fe94115(a)h20g2000vbs.googlegroups.com>...
> On Jul 26, 1:25 pm, "Malcolm McLean" <malcolm.mcle...(a)btinternet.com>
> wrote:
> > JimRockford<jim.rockfo...(a)gmail.com> wrote in message
> >
> > > mxArray *x[mystruct.n];     // doesn't work
> > > mxArray *x[nsize];            // doesn't work either
> > > mxArray *x[4];                 // this works, of course
> >
> > mxArray **x = mxMalloc(mystruct.n * sizeof(maArray *));
>
>
> Thanks for the reply (though I think you meant "mxArray" instead of
> "maArray"). However, what you propose does not work. I get the
> compilation error
>
> error C2440: 'initializing' : cannot convert from 'void *' to 'mxArray
> **'
> Conversion from 'void*' to pointer to non-'void' requires an
> explicit cast

That's because you are using C++ which requires a cast (C does not). So use a cast:

mxArray **x = (mxArray **) mxMalloc(mystruct.n * sizeof(maArray *));
:
(use x)
:
mxFree(x);


James Tursa
From: Jim Rockford on
On Jul 27, 11:32 am, "James Tursa"
<aclassyguy_with_a_k_not_...(a)hotmail.com> wrote:
> Jim Rockford <jim.rockfo...(a)gmail.com> wrote in message <fc540812-ab06-42b8-86c8-03809fe94...(a)h20g2000vbs.googlegroups.com>...
> > On Jul 26, 1:25 pm, "Malcolm McLean" <malcolm.mcle...(a)btinternet.com>
> > wrote:
> > > JimRockford<jim.rockfo...(a)gmail.com> wrote in message
>
> > > > mxArray *x[mystruct.n];     // doesn't work
> > > > mxArray *x[nsize];            // doesn't work either
> > > > mxArray *x[4];                 // this works, of course
>
> > > mxArray **x = mxMalloc(mystruct.n * sizeof(maArray *));
>
> > Thanks for the reply (though I think you meant "mxArray" instead of
> > "maArray").  However, what you propose does not work.  I get the
> > compilation error
>
> > error C2440: 'initializing' : cannot convert from 'void *' to 'mxArray
> > **'
> >         Conversion from 'void*' to pointer to non-'void' requires an
> > explicit cast
>
> That's because you are using C++ which requires a cast (C does not). So use a cast:
>
> mxArray **x = (mxArray **) mxMalloc(mystruct.n * sizeof(maArray *));
>     :
> (use x)
>     :
> mxFree(x);
>
> James Tursa


Oops. I should've stated that originally; my fault. Thanks James.

Jim