Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting An Array Of Strings From A Char** With FFI And Ruby

DZone's Guide to

Getting An Array Of Strings From A Char** With FFI And Ruby

·
Free Resource
This code adds a method to the FFI::Pointer class that makes it easy to unravel an array of null terminated strings from a char** type.


require 'ffi'

class FFI::Pointer
  def read_array_of_string
    elements = []

    loc = self

    until ((element = loc.read_pointer).null?)
     elements << element.read_string
     loc += FFI::Type::POINTER.size
    end

    elements
  end
end
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}