# Mapping betwing a standard type and a user defined type with SWIG

**URL:** <https://www.ruby-forum.com/t/mapping-betwing-a-standard-type-and-a-user-defined-type-with-swig/146394>\
**Category:** Ruby\
**Created:** [August 20, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/mapping-betwing-a-standard-type-and-a-user-defined-type-with-swig/146394 "2008-08-20T16:23:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [August 20, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/mapping-betwing-a-standard-type-and-a-user-defined-type-with-swig/146394/1 "2008-08-20T16:23:28Z")

</div>

Hello!

I’ve wrote a C library where I redefined (with typedef) the “char” and I  
gave it the name “my\_char”. Every things worked well when I gave  
constant  
strings to my wrapped functions which takes a “my\_char \*” as argument  
(then  
it is equivalent to give a char \* argument). But when I have given a  
String  
variable to one of my functions (I initialised a variable var = “” and  
gave  
it to the finction), it returned me this:

Expected argument 3 of type my\_char \*, but got String “” (TypeError)

How can I do to tell SWIG that the “my\_char” type is euivalent to the  
“char”  
type? May I have a solution to make work my functions

thank you

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [August 20, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/mapping-betwing-a-standard-type-and-a-user-defined-type-with-swig/146394/2 "2008-08-20T17:56:10Z")

</div>

Lyes A. wrote:

> type? May I have a solution to make work my functions  
> You are passing a C function that expects a ‘my\_char\*’ a Ruby string,  
> which in C has the type VALUE.

You need to apply a typemap that tells SWIG how to translate a ruby  
object into my\_char\*. Something roughly like:

%typemap(in) my\_char\* “$1 = (my\_char\*)STR2CSTR($input);”

This is the most basic conversion; a good typemap would probably also  
verify the ruby class of the passed-in argument, perhaps using SWIG’s  
%typemap(check)

Without wishing to be rude, this is fairly basic SWIG stuff. You might  
want to have another look at the manual. It is dense but quite  
comprehensive. In particular:

[http://www.swig.org/Doc1.3/Ruby.html#Ruby\_nn37](http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn37)  
[http://www.swig.org/Doc1.3/Ruby.html#Ruby\_nn41](http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn41)

alex

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [August 20, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/mapping-betwing-a-standard-type-and-a-user-defined-type-with-swig/146394/3 "2008-08-20T18:05:34Z")

</div>

On Wed, Aug 20, 2008 at 4:51 PM, Alex F.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > it to the finction), it returned me this:
> 
> to have another look at the manual. It is dense but quite comprehensive. In  
> particular:
> 
> [SWIG and Ruby](http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn37)  
> [SWIG and Ruby](http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn41)
> 
> alex

Hello

Thank you for your help, I will try this.
