# Ffi, struct pointer question

**URL:** <https://www.ruby-forum.com/t/ffi-struct-pointer-question/173091>\
**Category:** Ruby\
**Created:** [August 2, 2009, 7:11am UTC](https://www.ruby-forum.com/t/ffi-struct-pointer-question/173091 "2009-08-02T07:11:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 2, 2009, 7:11am UTC](https://www.ruby-forum.com/t/ffi-struct-pointer-question/173091/1 "2009-08-02T07:11:20Z")

</div>

Hi,

I’ve recently been trying to port some C code over to FFI. I’m trying  
to figure out how to unravel the gr\_mem group struct member. Any  
suggestions?

require ‘ffi’

module Sys  
class Admin  
extend FFI::Library

```
  class GroupStruct < FFI::Struct
     layout(
        :gr_name, :string,
        :gr_passwd, :string,
        :gr_gid, :int,
        :gr_mem, :pointer
     )
  end

  attach_function :getgrgid, [:int], :pointer

  def self.get_group(gid)
     GroupStruct.new(getgrgid(gid))
  end

```

end  
end

# How do I unravel the gr\_mem struct member?

struct = Sys::Admin.get\_group(84)  
p struct[:gr\_mem]

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 2, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/ffi-struct-pointer-question/173091/2 "2009-08-02T14:15:08Z")

</div>

2009/8/2 Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> def self.get_group(gid)
> GroupStruct.new(getgrgid(gid))
> end
> 
> ```
> 
> […]
> 
> # How do I unravel the gr\_mem struct member?
> 
> struct = Sys::Admin.get\_group(84)  
> p struct[:gr\_mem]

Hello,

the gr\_mem member of the group structure is a NULL terminated array of  
zero terminated strings, so you need to read all the string pointers  
from the array until you encounter a null pointer and then read the  
string data from each string pointer.

I would suggest the use of an auxiliary method, which could actually  
be added to the FFI::Pointer class for convenience:

class FFI::Pointer  
def read\_string\_array\_to\_null()  
elements = []

```
  psz = self.class.size
  loc = self
  until ((element = loc.read_pointer).null?)
    elements << element.read_string_to_null
    loc += psz
  end

  elements
end

```

end

Using this method, you can unravel the mysterious pointer member as  
follows:

p struct[:gr\_mem].read\_string\_array\_to\_null

😉

I hope that helps,  
Thomas

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 2, 2009, 4:08pm UTC](https://www.ruby-forum.com/t/ffi-struct-pointer-question/173091/3 "2009-08-02T16:08:05Z")

</div>

On Aug 2, 6:13 am, Thomas C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > end
> > 
> > ```
> 
> ```
> class FFI::Pointer
> elements
> 
> ```
> 
> Thomas  
> Thank you for that Thomas, it worked great.

I also found nice-ffi, and I suspect it may have some convenient way  
of doing what you’re doing there, only I don’t see it.

> **[GitHub - jacius/nice-ffi: Nice-FFI: Convenience layer atop Ruby-FFI](https://github.com/jacius/nice-ffi/tree/master)**
>
> master

Regards,

Dan
