title: How to wrap a C function whose parameters are pointer to structs, so that it can be called from Lua?
body: <p>I have the follwing C function. How should I wrap it so it can be called from a Lua script?</p>
  
  <pre><code>typedef struct tagT{
      int a ; 
      int b ;
  } type_t;
  
  int lib_a_f_4(type_t *t)
  {
       return t-&gt;a * t-&gt;b ;
  }
  </code></pre>
  
  <p>I know how to wrapr it if the function parameter type were <code>int</code> or <code>char *</code>. Should I use <code>table</code> type for a C structure?</p>
  
  <p>EDIT: I am using SWIG for the wraping , according to this <a href="http://www.swig.org/Doc1.3/Lua.html" rel="nofollow noreferrer">doc</a>, It seems that I should automatically have this function <code>new_type_t(2,3)</code> , but it is not the case.  </p>
  
  <blockquote>
    <p>If you wrap a C structure, it is also
    mapped to a Lua userdata. By adding a
    metatable to the userdata, this
    provides a very natural interface. For
    example,</p>
    
    <p><code>struct Point{   int x,y; };</code></p>
    
    <p>is used as follows:</p>
    
    <p><code>p=example.new_Point()</code>
     <code>p.x=3</code>
     <code>p.y=5</code>
     <code>print(p.x,p.y) 3       5</code></p>
    
    <p>Similar access is provided for unions
    and the data members of C++ classes. C
    structures are created using a
    function new_Point(), but for C++
    classes are created using just the
    name Point().</p>
  </blockquote>