# How to pass const char \*vect\[\] to C via Ruby/DL?

**URL:** <https://www.ruby-forum.com/t/how-to-pass-const-char-vect-to-c-via-ruby-dl/62270>\
**Category:** Ruby\
**Created:** [June 2, 2006, 2:25am UTC](https://www.ruby-forum.com/t/how-to-pass-const-char-vect-to-c-via-ruby-dl/62270 "2006-06-02T02:25:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas\_Haas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Andreas\_Haas](https://www.ruby-forum.com/u/Andreas_Haas)\
**Post date:** [June 2, 2006, 2:25am UTC](https://www.ruby-forum.com/t/how-to-pass-const-char-vect-to-c-via-ruby-dl/62270/1 "2006-06-02T02:25:43Z")

</div>

Hi all,

I’m having problems to understand how to build-up “const char \*vect[]”  
data for passing to a C function that I wish to call via Ruby/DL:

# -----------------

int setv(const char \*ids[])  
{  
while (\*ids) {  
printf(“setv: %s\n”, ids[0]);  
ids++;  
}  
}

# -----------------

My current Ruby program looks like this:

# -----------------

#!/usr/bin/ruby

require ‘dl/import’

def setv(ary)  
setv = $lib[‘setv’, ‘IP’]  
ids = DL.malloc(DL.sizeof(‘S’)\*(ary.size + 1))  
i = 0  
ary.each do |s|  
ids[DL.sizeof(‘S’)\*i] = s.to\_ptr  
i += 1  
end  
# ids[3] = nil ?  
r,r1 = setv.call(ids.ref)  
end

$lib = DL.dlopen(‘lib.dylib’)

setv([“1”, “2”, “3”])

# -----------------

but I get a SEGV and it is not complete anyways. Instead of

```
ids[DL.sizeof('S')*i] = s.to_ptr

```

I already tried

ids[DL.sizeof(‘S’)\*i] = s

and other variations I could think of …

You know: I can imagine there were better means for  
passing string vectors to C, yet the C function interface  
as such is given.

So any help or hint is greatly appreciated!

Best regards,  
Andreas

---

<div class="post-metadata">

**Author:** ![Andreas\_Haas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Andreas\_Haas](https://www.ruby-forum.com/u/Andreas_Haas)\
**Post date:** [June 2, 2006, 5:17am UTC](https://www.ruby-forum.com/t/how-to-pass-const-char-vect-to-c-via-ruby-dl/62270/2 "2006-06-02T05:17:55Z")

</div>

On Fri, 2 Jun 2006, Andreas Haas wrote:

> You know: I can imagine there were better means for  
> passing string vectors to C, yet the C function interface  
> as such is given.
> 
> So any help or hint is greatly appreciated!

i’m no dl expert, but:

harp:~ \> cat a.rb

# 

# generate the lib

# 

unless test(?e, ‘a.so’)  
open(‘a.c’,‘w’) do |f|  
f.write \<\<-‘code’  
int setv(const char \*ids[]){  
while(\*ids){ printf(“setv: %s\n”, ids[0]); ids++; }  
printf(“setv: %s\n”, ids[0]);  
}  
code  
end  
system “gcc -shared a.c -o a.so”  
end

require ‘dl/import’

def setv(ary)  
setv = $lib[‘setv’, ‘IP’]  
ids = ary.map{|s| s.to\_ptr}  
ids \<\< DL::PtrData.new(0)  
r,r1 = setv.call ids.to\_ptr  
end

$lib = DL.dlopen(’./a.so’)

setv %w( 1 2 3 )

harp:~ \> ruby a.rb  
setv: 1  
setv: 2  
setv: 3  
setv: (null)

regards.

-a

---

<div class="post-metadata">

**Author:** ![Andreas\_Haas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Andreas\_Haas](https://www.ruby-forum.com/u/Andreas_Haas)\
**Post date:** [June 2, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/how-to-pass-const-char-vect-to-c-via-ruby-dl/62270/3 "2006-06-02T14:56:35Z")

</div>

Am Freitag, 02.06.06 um 05:14 Uhr schrieb removed\_email\_address@domain.invalid:

> i’m no dl expert, but:
> 
> def setv(ary)  
> setv = $lib[‘setv’, ‘IP’]  
> ids = ary.map{|s| s.to\_ptr}  
> ids \<\< DL::PtrData.new(0)  
> r,r1 = setv.call ids.to\_ptr  
> end

Now it works.

Thanks Ara!

Regards,  
Andreas
