NATURAL calling C function : how to pass a double parm ?

Hi all,

I need to call a C function from NATURAL (UNIX) with the following parms :

C-LIBELLE A8
C-MODE F8
C-X1 F8
C-X2 F8
C-CODE A3
C-SIZE N3
C-ARRAY F8 (100)
C-TAPE P2
C-ALERT I4

The C function uses the corresponding variables below :

    char LIBELLE[16];
    double MODE;
    double X1;
    double X2;
    char CODE[3];  
    int ARRAYSIZE;
    double ARRAY[100];
    int TAPE;                
    int ALERT;

The C function is part of a dynamic library which was build following the instructions of SAG doc (use of natuser.h, jumptab.c, ncuxinit.c and Makedyn makefile)
I would like to know if it is possible, and how :wink: , to pass the NATURAL parms to the C program.
In the C source code i redefined all the variables with string format. Then i use memmove function to copy the adress of the original NATURAL parm to the address of the string variable in C :

memmove ((char *) &param, (char *) parmptr[i], sizeof(param)-1);
and then i try to cast the string but it does not work for float parms nor Numerical fields type N with decimals. (it works with I4 and N3.0 for example)

If you have an example on your desk…
Any help would be appreciated.
Thank you.

If N3.0 works, you should be able to get, for example, N5.3 to work. “Pass it” as N8.0 and divide by 1000 when you get to C. Remember, there is no decimal point in a Natural N5.3, just eight digits.

steve

Hi Steve,

Thanks for your answer. It’s OK for format N. Do you have any idea for the format F8 ? The hexadecimal value i get in C doesn’t seem to mean anything for a double type. I don’t know if i am making a mistake while passing( or retrieving) arguments to C program or if the internal format of FLOAT number in NATURAL is different than the corresponding double format in C…

Regards,

have you tried a “long double” for the F8 fields?

Hello,

Sorry for the delay but i found my error. The right format in C is double, with long double type i got other errors.
This syntax works fine :

memmove ((double *) &double1, (char *) parmptr[6], sizeof(double *));

For information (and may be it could help someone else…) My error was in the use of a memmove and a strtol() statements with a string one byte too short so i didn’t have the ‘/0’ character i was expecting. After that the alignment in memory was wrong… :oops: