Creating plain C array of structs

Showing 1-3 of 3 messages
Creating plain C array of structs Richard Venneman 10/29/12 5:26 AM

I'm trying to create a MKPolyline with the MKPolyline#polylineWithCoordinates method. This method takes an array with coords as a parameter. I created an array like this:

coordinates = annotations.collect { |a| a.coordinate }

When I try to use this array with the polylineWithCoordinates method, the app crashes with the following error message:

2012-10-29 13:19:31.986 Cityguide[26645:c07] *** Terminating app due to uncaught exception 'TypeError', reason: 'path_map_view_controller.rb:38:in `drawPath': expected instance of Pointer, got `[#<CLLocationCoordinate2D latitude=53.1852416992188 longitude=5.54879379272461>, #<CLLocationCoordinate2D latitude=53.1885986328125 longitude=5.555419921875>]' (Array) (TypeError)

From this answer on SO, that apparently happens because of this reason: The polylineWithCoordinates method requires a plain C array of structs of typeCLLocationCoordinate2D.

Usually these plain C arrays apparently get created with malloc(). But that doesn't work in RubyMotion. So can anyone help me making polylineWithCoordinates work with the correct array type?

Thanks :)
Re: [RubyMotion] Creating plain C array of structs Colin T.A. Gray 10/29/12 6:56 AM
You'll need to use the `Pointer` class (

One example in particular from that page: rect_ptr =

An optional second argument is the 'length'.  So in your case, I'm pretty sure this is what you need:

good_real_estate = [location, location, location]
ptr =, good_real_estate.length)
ptr[0] = good_real_estate[0]
ptr[1] = good_real_estate[1]
ptr[2] = good_real_estate[2]

And there is a sugarcube helper for this!

good_real_estate = [location, location, location]
ptr = good_real_estate.to_pointer(CLLocation2D.type)


Re: [RubyMotion] Creating plain C array of structs Richard Venneman 10/29/12 7:22 AM
Thanks a lot Colin!
Creating a pointer with the CLLocationCoordinate2D type works perfectly.