CGO: how to return double array to go from c function?

1,094 views
Skip to first unread message

Curtis Paul

unread,
Feb 5, 2014, 1:14:46 AM2/5/14
to golan...@googlegroups.com
Any ideas on how to properly do this?

/*
double* the_c_function()
{
     // bunch of stuff happens to populate values[]

     double *values_ptr = mxGetPr(values[0]);   //this is a Matlab c function

     printf("an element value: %lf\n", values_ptr[3]);

     return values_ptr;
}
*/
import "C"


func main () {
     returned_double_array = C.the_c_function()

     //how do I get/read the elements of returned_double_array ?
}

Gustavo Niemeyer

unread,
Feb 5, 2014, 1:28:24 AM2/5/14
to Curtis Paul, golan...@googlegroups.com
On Wed, Feb 5, 2014 at 4:14 AM, Curtis Paul <curti...@gmail.com> wrote:
> Any ideas on how to properly do this?

Something along these lines: http://play.golang.org/p/ekbp7xHwQx


gustavo @ http://niemeyer.net

minux

unread,
Feb 5, 2014, 1:35:42 AM2/5/14
to Curtis Paul, golang-nuts
You can convert it to a Go slice of float64.

Curtis Paul

unread,
Feb 5, 2014, 9:22:25 PM2/5/14
to golan...@googlegroups.com
So far the examples have been what's documented, and doesn't really answer my question.  At least not in a way I understand.

Curtis Paul

unread,
Feb 5, 2014, 10:47:49 PM2/5/14
to golan...@googlegroups.com
So I'm able to get the elements of the array, but they are scientific notiation, and when I try doing an fmt.Println("%2.1f",Cdoublefield) I get 0.0 for any of the values.

fmt.Println(Cdoublefield) prints 8.637783107073355e-270  which is supposed to be 23.2 ...why would I get 0.0 instead of 23.2?


On Tuesday, February 4, 2014 11:14:46 PM UTC-7, Curtis Paul wrote:

Curtis Paul

unread,
Feb 5, 2014, 11:55:53 PM2/5/14
to golan...@googlegroups.com
No, it's just returning the correct value...it's not a scientific notation representation of the returned value, it's just bad data.

Does anyone know if C lang can return a double array to Go correctly?  Can Go interpret a C lang double array?

I have not been able to find any examples of doing this anywhere.....I see string and integer examples....but no float or double examples...

On Tuesday, February 4, 2014 11:14:46 PM UTC-7, Curtis Paul wrote:

Curtis Paul

unread,
Feb 6, 2014, 12:01:28 AM2/6/14
to golan...@googlegroups.com
I mean it's not returning the correct double value

Dan Kortschak

unread,
Feb 6, 2014, 12:18:17 AM2/6/14
to Curtis Paul, golan...@googlegroups.com
On Wed, 2014-02-05 at 21:01 -0800, Curtis Paul wrote:
> I mean it's not returning the correct double value

You're not passing the double to foo, so it's getting uninitialised
data.

Dan Kortschak

unread,
Feb 6, 2014, 12:20:41 AM2/6/14
to Curtis Paul, golan...@googlegroups.com

Curtis Paul

unread,
Feb 6, 2014, 12:23:07 AM2/6/14
to golan...@googlegroups.com, Curtis Paul
I got it now, makes sense....I appreciate the help, thanks!

On Wednesday, February 5, 2014 10:20:41 PM UTC-7, kortschak wrote:
http://play.golang.org/p/sijNOPw6o4 sorry.

Reply all
Reply to author
Forward
0 new messages