# Question on Interfacing and Exterfacing KDB with C

**URL:** https://forum.kx.com/t/question-on-interfacing-and-exterfacing-kdb-with-c/7161
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [May 25, 2011, 4:35pm UTC](https://forum.kx.com/t/question-on-interfacing-and-exterfacing-kdb-with-c/7161 "2011-05-25T16:35:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![tchan](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@tchan](https://forum.kx.com/u/tchan)
#### Post date: [May 25, 2011, 4:35pm UTC](https://forum.kx.com/t/question-on-interfacing-and-exterfacing-kdb-with-c/7161/1 "2011-05-25T16:35:00Z")

</div>

Hi all,I am new to KDB development, currrently I have two questions onInterfacing and Exterfacing KDB with C.While reading the example and test the sample program myself, I foundout that I am not able to pass two parameters into a function like:K foo(K x, K y){…}if I want to use this C function in Q with four parameters: oneinteger list, one string list, one integer and one long value, how canI achieve it??And my second question is when I pass a list to C function like this:K foo(K x){…}, how can I read all the elements from list? And isthere any efficient way to pass a K type list to a C type array,forexample int\* or int from a int list in KDB??Thanks

---

<div class="post-metadata">

### Author: ![tchan](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@tchan](https://forum.kx.com/u/tchan)
#### Post date: [May 27, 2011, 5:23am UTC](https://forum.kx.com/t/question-on-interfacing-and-exterfacing-kdb-with-c/7161/2 "2011-05-27T05:23:00Z")

</div>

Anybody knows how to work it out?

---

<div class="post-metadata">

### Author: ![trieder](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@trieder](https://forum.kx.com/u/trieder)
#### Post date: [May 27, 2011, 6:37am UTC](https://forum.kx.com/t/question-on-interfacing-and-exterfacing-kdb-with-c/7161/3 "2011-05-27T06:37:00Z")

</div>

charset=us-ascii  
X-Mailer: iPhone Mail (8F190)  
In-Reply-To: \<64cc3b08-4cc0-4962-88fb-1034d41e7e43@h36g2000pro.googlegroups.com\>  
Message-Id:   
Date: Fri, 27 May 2011 01:37:08 -0400  
To: “[personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)”   
  
Mime-Version: 1.0 (iPhone Mail 8F190)  
  
Your questions can be answered with a careful reading of the following two p=  
ages:=20  
  
[https://code.kx.com/trac/wiki/Cookbook/ExtendingWithC](https://code.kx.com/trac/wiki/Cookbook/ExtendingWithC)  
  
[https://code.kx.com/trac/wiki/Cookbook/InterfacingWithC](https://code.kx.com/trac/wiki/Cookbook/InterfacingWithC)  
  
Im not exactly sure what you mean by not being able to pass in two, yet you w=  
ant to pass in four, but here’s what you can do:  
  
1) have the function accept a dictionary or a list and pass all of your para=  
meters in that way i.e.  
  
K foo(K x) {…}  
  
2) give each input its own argument i.e.  
  
K foo(K x, K y, K z, K w) {…}  
  
To access an array of a particular type, see the interfacing with C page and=  
 the large chart which contains types and vector accessors on it.  
  
Once you know the accessors/how they work you can easily extract the data to=  
 a c array if you want.

---

<div class="post-metadata">

### Author: ![tchan](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@tchan](https://forum.kx.com/u/tchan)
#### Post date: [May 30, 2011, 8:00am UTC](https://forum.kx.com/t/question-on-interfacing-and-exterfacing-kdb-with-c/7161/4 "2011-05-30T08:00:00Z")

</div>

Hi Timothy,Thanks for your reply.I have tried the methods from the two pages of cookbook. But I stillhave some problem with it.The function I want to implement should be able to handle three lists:integer, float(c double) and char.and currently, I have written a C function like:K myFun(K x, K y){ printf(“%i\n”,kI(x)[0]); printf(“%i\n”,kI(x)[1]); printf(“%i\n”,kF(y)[0]); printf(“%i\n”,kF(y)[1]); //printf(“%i\n”,kI(y)[0]); //printf(“%i\n”,kI(y)[1]); return ki((I)0);}in which the x accept a integer list and y accept a float(c double)listand I call them in q by:q\>a:1 2q\>b:5 3.9q\>myFun a band I got a type errorThen I have tried second setting:I have tried to pass in two integer list while I uncomment line 6,7with line 4,5 comment:q\>a:1 2q\>b:3 4q\>myFun[a;b]'rankq\>myFun a bq\>-2147483648q\>-2147483648q\>65648q\>79695952which seems the address pointerI either got a rank error or some pointer addressin this case, how can I pass into lists in K x and K y into thefunction.Also, if I pass in something as following in the second setting:q\>myFun 5 4 3 2 1it returns me:546564879695952May I ask why is it happen and how can I solve it?really appreciate for your helpOn 5??27??, ?U??1??37??, Timothy Rieder \<trie…\> wrote:\> Your questions can be answered with a careful reading of the following two pages:\>\> [https://code.kx.com/trac/wiki/Cookbook/ExtendingWithC&gt;&gt](https://code.kx.com/trac/wiki/Cookbook/ExtendingWithC&gt;&gt); [https://code.kx.com/trac/wiki/Cookbook/InterfacingWithC&gt;&gt](https://code.kx.com/trac/wiki/Cookbook/InterfacingWithC&gt;&gt); Im not exactly sure what you mean by not being able to pass in two, yet you want to pass in four, but here’s what you can do:\>\> 1) have the function accept a dictionary or a list and pass all of your parameters in that way i.e.\>\> K foo(K x) {…}\>\> 2) give each input its own argument i.e.\>\> K foo(K x, K y, K z, K w) {…}\>\> To access an array of a particular type, see the interfacing with C page and the large chart which contains types and vector accessors on it.\>\> Once you know the accessors/how they work you can easily extract the data to a c array if you want.\>\> On May 25, 2011, at 11:35 AM, tchan \<qgpx2…\> wrote:\>\>\>\> \> Hi all,\>\> \> I am new to KDB development, currrently I have two questions on\> \> Interfacing and Exterfacing KDB with C.\> \> While reading the example and test the sample program myself, I found\> \> out that I am not able to pass two parameters into a function like:\> \> K foo(K x, K y){…}\> \> if I want to use this C function in Q with four parameters: one\> \> integer list, one string list, one integer and one long value, how can\> \> I achieve it??\>\> \> And my second question is when I pass a list to C function like this:\> \> K foo(K x){…}, how can I read all the elements from list? And is\> \> there any efficient way to pass a K type list to a C type array,for\> \> example int\* or int from a int list in KDB??\>\> \> Thanks\>\> \> –\> \>

Submitted via Google Groups\</qgpx2…\>\</trie…\>

---

<div class="post-metadata">

### Author: ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)
#### Post date: [May 30, 2011, 8:11am UTC](https://forum.kx.com/t/question-on-interfacing-and-exterfacing-kdb-with-c/7161/5 "2011-05-30T08:11:00Z")

</div>

The type error comes from trying to apply b to a with this notation myFun a b.

You should call it with&nbsp;myFun[a;b]

The rank error comes from trying to call the c function with more arguments than it expects. When you bind the c function to q, you use

myFun:`nameoflibfile 2:(`nativeFunctionName;numberOfArgs)

I expect you have 1 for numberOfArgs where as you need 2 for your example.

thanks

2011/5/30 tchan \<qgpx2006@gmail.com\>

> Hi Timothy,
> 
> Thanks for your reply.
> 
> I have tried the methods from the two pages of cookbook. But I still  
> have some problem with it.
> 
> The function I want to implement should be able to handle three lists:  
> integer, float(c double) and char.
> 
> and currently, I have written a C function like:  
> K myFun(K x, K y){  
> &nbsp; &nbsp; &nbsp; &nbsp;printf(“%i\n”,kI(x)[0]);  
> &nbsp; &nbsp; &nbsp; &nbsp;printf(“%i\n”,kI(x)[1]);  
> &nbsp; &nbsp; &nbsp; &nbsp;printf(“%i\n”,kF(y)[0]);  
> &nbsp; &nbsp; &nbsp; &nbsp;printf(“%i\n”,kF(y)[1]);  
> &nbsp; &nbsp; &nbsp; &nbsp;//printf(“%i\n”,kI(y)[0]);  
> &nbsp; &nbsp; &nbsp; &nbsp;//printf(“%i\n”,kI(y)[1]);  
> &nbsp; &nbsp; &nbsp; &nbsp;return ki((I)0);  
> }
> 
> in which the x accept a integer list and y accept a float(c double)  
> list  
> and I call them in q by:  
> q\>a:1 2  
> q\>b:5 3.9  
> q\>myFun a b
> 
> and I got a type error
> 
> Then I have tried second setting:  
> I have tried to pass in two integer list while I uncomment line 6,7  
> with line 4,5 comment:
> 
> q\>a:1 2  
> q\>b:3 4  
> q\>myFun[a;b]  
> 'rank
> 
> q\>myFun a b  
> q\>-2147483648  
> q\>-2147483648  
> q\>65648  
> q\>79695952  
> which seems the address pointer
> 
> I either got a rank error or some pointer address  
> in this case, how can I pass into lists in K x and K y into the  
> function.  
> Also, if I pass in something as following in the second setting:  
> q\>myFun 5 4 3 2 1
> 
> it returns me:  
> 5  
> 4  
> 65648  
> 79695952
> 
> May I ask why is it happen and how can I solve it?
> 
> really appreciate for your help

---

<div class="post-metadata">

### Author: ![tchan](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@tchan](https://forum.kx.com/u/tchan)
#### Post date: [May 30, 2011, 9:15am UTC](https://forum.kx.com/t/question-on-interfacing-and-exterfacing-kdb-with-c/7161/6 "2011-05-30T09:15:00Z")

</div>

Hi Charles,Really thanks for your adviceTomOn 5?30?, ??3?11?, Charles Skelton \<char…\> wrote:\> The type error comes from trying to apply b to a with this notation myFun a\> b.\>\> You should call it with myFun[a;b]\>\> The rank error comes from trying to call the c function with more arguments\> than it expects. When you bind the c function to q, you use\>\> myFun:`nameoflibfile 2:(`nativeFunctionName;numberOfArgs)\>\> I expect you have 1 for numberOfArgs where as you need 2 for your example.\>\> thanks\>\> 2011/5/30 tchan \<qgpx2…\>\>\>\>\> \> Hi Timothy,\>\> \> Thanks for your reply.\>\> \> I have tried the methods from the two pages of cookbook. But I still\> \> have some problem with it.\>\> \> The function I want to implement should be able to handle three lists:\> \> integer, float(c double) and char.\>\> \> and currently, I have written a C function like:\> \> K myFun(K x, K y){\> \> printf(“%i\n”,kI(x)[0]);\> \> printf(“%i\n”,kI(x)[1]);\> \> printf(“%i\n”,kF(y)[0]);\> \> printf(“%i\n”,kF(y)[1]);\> \> //printf(“%i\n”,kI(y)[0]);\> \> //printf(“%i\n”,kI(y)[1]);\> \> return ki((I)0);\> \> }\>\> \> in which the x accept a integer list and y accept a float(c double)\> \> list\> \> and I call them in q by:\> \> q\>a:1 2\> \> q\>b:5 3.9\> \> q\>myFun a b\>\> \> and I got a type error\>\> \> Then I have tried second setting:\> \> I have tried to pass in two integer list while I uncomment line 6,7\> \> with line 4,5 comment:\>\> \> q\>a:1 2\> \> q\>b:3 4\> \> q\>myFun[a;b]\> \> 'rank\>\> \> q\>myFun a b\> \> q\>-2147483648\> \> q\>-2147483648\> \> q\>65648\> \> q\>79695952\> \> which seems the address pointer\>\> \> I either got a rank error or some pointer address\> \> in this case, how can I pass into lists in K x and K y into the\> \> function.\> \> Also, if I pass in something as following in the second setting:\> \> q\>myFun 5 4 3 2 1\>\> \> it returns me:\> \> 5\> \> 4\> \> 65648\> \> 79695952\>\> \> May I ask why is it happen and how can I solve it?\>\> \> really appreciate for your help\>\> \> On 5?27?, ??1?37?, Timothy Rieder \<trie…\> wrote:\> \> \> Your questions can be answered with a careful reading of the following\> \> two pages:\>\> \> \>[https://code.kx.com/trac/wiki/Cookbook/ExtendingWithC&gt;&gt](https://code.kx.com/trac/wiki/Cookbook/ExtendingWithC&gt;&gt); \> \>[https://code.kx.com/trac/wiki/Cookbook/InterfacingWithC&gt;&gt](https://code.kx.com/trac/wiki/Cookbook/InterfacingWithC&gt;&gt); \> \> Im not exactly sure what you mean by not being able to pass in two, yet\> \> you want to pass in four, but here’s what you can do:\>\> \> \> 1) have the function accept a dictionary or a list and pass all of your\> \> parameters in that way i.e.\>\> \> \> K foo(K x) {…}\>\> \> \> 2) give each input its own argument i.e.\>\> \> \> K foo(K x, K y, K z, K w) {…}\>\> \> \> To access an array of a particular type, see the interfacing with C page\> \> and the large chart which contains types and vector accessors on it.\>\> \> \> Once you know the accessors/how they work you can easily extract the data\> \> to a c array if you want.\>\> \> \> On May 25, 2011, at 11:35 AM, tchan \<qgpx2…\> wrote:\>\> \> \> \> Hi all,\>\> \> \> \> I am new to KDB development, currrently I have two questions on\> \> \> \> Interfacing and Exterfacing KDB with C.\> \> \> \> While reading the example and test the sample program myself, I found\> \> \> \> out that I am not able to pass two parameters into a function like:\> \> \> \> K foo(K x, K y){…}\> \> \> \> if I want to use this C function in Q with four parameters: one\> \> \> \> integer list, one string list, one integer and one long value, how can\> \> \> \> I achieve it??\>\> \> \> \> And my second question is when I pass a list to C function like this:\> \> \> \> K foo(K x){…}, how can I read all the elements from list? And is\> \> \> \> there any efficient way to pass a K type list to a C type array,for\> \> \> \> example int\* or int from a int list in KDB??\>\> \> \> \> Thanks\>\> \> \> \> –\> \> \> \> You received this message because you are subscribed to the Google\> \> Groups “Kdb+ Personal Developers” group.\> \> \> \> To post to this group, send email to [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)\> \> .\> \> \> \> To unsubscribe from this group, send email to\> \> [personal-kdbplus+unsubscribe@googlegroups.com](mailto:personal-kdbplus+unsubscribe@googlegroups.com).\> \> \> \> For more options, visit this group athttp://\> \> [groups.google.com/group/personal-kdbplus?hl=en.-](http://groups.google.com/group/personal-kdbplus?hl=en.-) ??? -\>\> \> \> - ??? -\>\> \> –\> \>

Submitted via Google Groups\</qgpx2…\>\</trie…\>\</qgpx2…\>\</char…\>
