From: Takaaki Tateishi Date: 2002-11-26T20:11:18+09:00 Subject: Re: DL and arrays of pointers At Tue, 26 Nov 2002 17:55:46 +0900, > > typedef _foo { > > char * bar; > > unsigned int qux; > > } Foo; > > > > Foo ** foos; > > I tried to define foos = struct[ "Food ** foos" ] but i'm not able to > access foos[0] at a later time. "struct[...]" creates something like a wrapper class for struct Foo. The following example shows how to access foos[0], foos[1],... --- test.c --- #include #include typedef struct { char bar[10]; unsigned int qux; } Foo; /* create_foos() creates Foos */ Foo **create_foos(int n){ Foo **foos; int i; foos = (Foo**)malloc(sizeof(Foo*) * n); for( i=0; ibar,"%d",i); foos[i]->qux = i; } return foos; } -- test.rb -- require 'dl/import' require 'dl/struct' module Foo extend DL::Importable dlload 'test.so' typealias "Foo", "void*" extern "Foo **create_foos(int)" Foo = struct [ "char bar[10]", "int qux", ] end n = 10 foos = Foo::create_foos(n) ptr = foos # "ptr = (void*)(foos)" in C layer for i in 0..(n-1) foo = ptr.ptr # "foo = *ptr" in C layer foo = Foo::Foo.new(foo) # wrap the pointer p [foo.bar.collect{|c| c.chr}.join, foo.qux] ptr += DL.sizeof("P") # increment the pointer end -- Takaaki Tateishi