[mercury-users] Passing structures from and to C

Jonathan Morgan jonmmorgan at gmail.com
Wed Jul 11 01:06:30 AEST 2007


On 7/11/07, Alexsandro Soares <a_s_soares at yahoo.com.br> wrote:
>    I'm trying to figure out how to pass structures from Mercury to C and back again. Supose I have the following C declaration:
>
>                typedef struct {int age; char *name;} person;


Look in chapter 13 of the reference manual, where the foreign language
interface is described.

>    I would like:
>    1 - to declare this structure in Mercury;

Declare it with `pragma foreign_type'.  You may also need to include
the type's definition through headers (use `pragma foreign_decl') or
define the type in the module (use `pragma foreign_code').

>    2 - to create a C function that receives an int and a string and returns this structure filled to Mercury;
>    3 - to create another C function that receives this structure from Mercury and returns the field "name".

Look at `pragma foreign_proc'.  If you use a foreign_type, the type
will be passed to C as that C type, so the C foreign procedure will be
able to manipulate it directly in an ordinary manner.  For the third
one you might also want to look at the record syntax, to allow you to
do:

Name = Struct ^ name

rather than:

Name = name(Struct)

which I prefer, though you may not.

Jon
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list