Re: MathLink MLGetRealArray
- To: mathgroup at smc.vnet.net
- Subject: [mg21971] Re: MathLink MLGetRealArray
- From: Eckhard Hennig <hennig at itwm.uni-kl.de>
- Date: Mon, 7 Feb 2000 13:02:25 -0500 (EST)
- Organization: ITWM
- References: <87iph4$3vb@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Johannes Ludsteck schrieb in Nachricht <87iph4$3vb at smc.vnet.net>...
>Dear Group Members,
>I am trying to get a matrix from Mathematica into a c program via
>MathLink. When I compile the code below (with Borland C++ 5.0), I
>get an Warning message Suspicious pointer conversion in line...,
>which relates to depthx. However, the variable depthx works fine.
>
>The problem is with the array x. If I try to access the elements in x,
>for example
>temp = x[0][0]; temp = temp * temp;
>
>then the program crashes.
[...]
>
>void kerndens(void)
>{
> double **x, temp;
> long *dimsx, depthx;
> char **headsx;
>
> MLGetRealArray(stdlink, &x, &dimsx, &headsx, &depthx);
>
> MLPutReal(stdlink, x[0][0]);
>
> MLDisownRealArray(stdlink, x, dimsx, headsx, depthx);
>}
Johannes,
at a first glance, it seems that the array x is declared incorrectly: The
pointer should be declared as
double *x; // Not: **x
What you get from a MathLink connection is a *linear* array of numbers and
not a two-dimensional one. Therefore, the C code needed to access the array
element x[i, j] is given by
xij = x[i*n + j]
where n = dimsx[1] is the column dimension. Please try if the following
modifications of your code help to solve your problem (I haven't tested
them, but the code should work).
void kerndens(void)
{
double *x, temp;
long *dimsx, depthx;
char **headsx;
int i, j, n;
MLGetRealArray(stdlink, &x, &dimsx, &headsx, &depthx);
n = dimsx[1];
i = 0;
j = 0;
MLPutReal(stdlink, x[i*n + j]);
MLDisownRealArray(stdlink, x, dimsx, headsx, depthx);
}
Best regards,
Eckhard
-----------------------------------------------------------
Dipl.-Ing. Eckhard Hennig mailto:hennig at itwm.uni-kl.de
Institut fuer Techno- und Wirtschaftsmathematik e.V. (ITWM)
Erwin-Schroedinger-Strasse, 67663 Kaiserslautern, Germany
Voice: +49-(0)631-205-3126 Fax: +49-(0)631-205-4139
http://www.itwm.uni-kl.de/as/employees/hennig.html
ITWM - Makers of Analog Insydes for Mathematica
http://www.itwm.uni-kl.de/as/products/ai
-----------------------------------------------------------