Re: CALL_EXTERNAL and structures [message #9325] |
Mon, 23 June 1997 00:00 |
Jeff Kommers
Messages: 2 Registered: June 1997
|
Junior Member |
|
|
> Hubert Rehrauer <rehrauer@vision.ee.ethz.ch>
> In the following example the values of the tag 'second' can not be
> modified and I can't see why!
I think the problem is in the way you defined the structure in
'idltest.c':
> typedef struct {
> long first;
> double * second;
> } data;
If your IDL structure is defined like this
IDL> data = {first:0L, second:dblarr(4)}
then in your C program you would want to define the same structure.
That means the second tag should be a double, *not* a pointer to a
double. In fact, if data.second always has 4 or fewer elements, then
in C the second tag should be a "double second[4];".
But assuming that IDL is responsible for allocating memory for the
data structure, and that your C source code does not know how many
elements data.second is going to have, you could do something like
this:
/* content of the file 'idltestfix.c' */
typedef struct {
long first;
double second; /* Actually the first element in an array of doubles */
/* See IDL code */
} data;
long idltestfix( int argc, void * argv[])
{
data * d;
double * psecond;
d = (data * ) argv[0];
d->first = 25;
psecond = &d->second;
*psecond = 1.4;
*(psecond+2) =3.8;
return(0L);
}
On SunOS 4.1.3 with IDL 5.0 I did the following
IDL> $ acc -c -pic idltestfix.c
IDL> $ ld -o idltestfix.so -assert pure-text idltestfix.o
IDL> a = 5l
IDL> b = dblarr(4)
IDL> data = {first:a,second:b}
IDL> print, data
{ 5 0.0000000 0.0000000 0.0000000 0.0000000
}
IDL> check = call_external('idltestfix.so','idltestfix',data)
IDL> print, data
{ 25 1.4000000 0.0000000 3.8000000 0.0000000
}
Good luck
Jeff
|
|
|
Re: CALL_EXTERNAL and structures [message #9326 is a reply to message #9325] |
Mon, 23 June 1997 00:00  |
rivers
Messages: 228 Registered: March 1991
|
Senior Member |
|
|
In article <33AE2A0D.481@vision.ee.ethz.ch>, Hubert Rehrauer <rehrauer@vision.ee.ethz.ch> writes:
> Hello,
>
> I got the following problem. I want to call a C function that modifies a
> IDL structure. This works pretty well, except for structure tags that
> have an array-type.
> /*
> IDL input:
>
> $ cc -G -Kpic -c -lm -v idltest.c
> $ ld -G -o idltest.so idltest.o
> a= 5L
> b = DBLARR(4)
> data ={ first: a, second: b }
...
> /* content of the file 'idltest.c' */
> typedef struct {
> long first;
> double * second;
> } data;
The declaration "double * second" is your problem. "second" is not a pointer
to a double, it is an array of doubles, i.e. the array occupies memory
immediately after the long "first", not at a location pointed to by a pointer
after "first". I have not tried it, but I predict that if you change the
declation and code to:
typedef struct {
long first;
double second[4];
} data;
...
d->first = 25;
d->second[0] = 1.4;
d->second[2] =3.8;
that it will work.
____________________________________________________________
Mark Rivers (773) 702-2279 (office)
CARS (773) 702-9951 (secretary)
Univ. of Chicago (773) 702-5454 (FAX)
5640 S. Ellis Ave. (708) 922-0499 (home)
Chicago, IL 60637 rivers@cars.uchicago.edu (e-mail)
or:
Argonne National Laboratory (630) 252-0422 (office)
Building 434A (630) 252-0405 (lab)
9700 South Cass Avenue (630) 252-1713 (beamline)
Argonne, IL 60439 (630) 252-0443 (FAX)
|
|
|