Your question, and future discussion, is more appropriate to the
programming forum.
This is a messy area and alignment and compiler differences are a
complication.
Here is some handwaving. Let the forum know if you need more.
tohex =: [: ,"2 ' ' (,"1) '0123456789ABCDEF' {~ 16 16 #: a. i. ]
d=. 2.3 2.4
_2{.3!:3 d
i=. 23
_1{.3!:3 i
s=. (2 fc d),2 ic i
tohex s
66 66 66 66 66 66 02 40 33 33 33 33 33 33 03 40 17 00 00 00
NB. s has the bit pattern you want
NB. call your routine as: ('.... n *c' cd s NB. no result, pointer to char
data that is your struct data
On Tue, Oct 5, 2021 at 1:36 PM Raul Miller <[email protected]> wrote:
> C structs are a bit tricky because different compilers, and different
> compiler options, can result in different layouts in memory for the
> structs.
>
> That said, something like this looks like it should be straightforward
> (I expect that most compilers would not introduce padding between
> members of the struct).
>
> FYI,
>
> --
> Raul
>
> On Tue, Oct 5, 2021 at 1:33 PM 'robert therriault' via General
> <[email protected]> wrote:
> >
> > Alex,
> >
> > I haven't done very much of that myself, but have you looked at chapter
> 21 of JforC?
> https://www.jsoftware.com/help/jforc/calling_external_programs.htm#_Toc191734429
> >
> > Cheers, bob
> >
> > > On Oct 5, 2021, at 09:21, Alex Shroyer <[email protected]> wrote:
> > >
> > > How do I send/receive data structures to/from C functions using the cd
> verb?
> > >
> > > For example if I have this C library saved as /tmp/example.dylib, how
> do I
> > > call it from J?
> > >
> > > // C code:
> > > struct stuff {double a[2]; int i;};
> > > void fn(struct stuff *x) {x->a[x->i] += 100;}
> > >
> > > NB. J code:
> > > data =: (2.3 4.5);0 NB. the data I would like to send to fn (array
> of 2
> > > doubles; int)
> > > '/tmp/example.dylib fn (declaration)' cd (parameters)
> > >
> > > Specifically, what do I use for the (declaration) and what do I use for
> > > (parameters)?
> > > I presume I should use something from
> > > https://www.jsoftware.com/help/dictionary/dx003.htm but I'm unsure
> how to
> > > then specify it in the (declaration).
> > >
> > > Is there a relevant example somewhere?
> > >
> > > Thanks,
> > > Alex
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm