Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Passing a table from Lua into C

In a previous article, I showed you a simple example of how you can call a function that's written in C from Lua, and a second example that extended that - passing simple parameters in to the C function, and returning results.

But it's often more complex that that - for example, you may with to pass a table from your Lua script into a C function, so that the C function can make use of a value from the table code like this:

stuff = {hotel = 48, hq = 404, town = 1}
summat = extratasks.dothis(stuff)


This isn't as easy as just passing a value, since the size of a table can vary, and Lua's dynamic memory allocation doesn't sit well, in a simple interface, with C's static model. The "trick" is to tackle it within the C code by using function calls to get (and if necessary) reset values from the table - with the data required by those function calls being processed via the Lua stack.

Here's an example of what I mean in the C code:

/* Looking up based on the key */
/* Add key we're interested in to the stack*/
lua_pushstring(L,"hq");
/* Have Lua functions look up the keyed value */
lua_gettable(L, -2 );
/* Extract the result which is on the stack */
double workon = lua_tonumber(L,-1);
/* Tidy up the stack - get rid of the extra we added*/
lua_pop(L,1);


That code retrieves the "hq" value from the table that's been passed in (called stuff in my Lua example above) and stores it into a C variable called workon ... objective achieved!

The full source code of the Lua is available here and the C source code is here.

Here's sample output from my testing of the code:

[trainee@easterton three]$ lua wrapper.lua
Lua code running ...
Violets are Blue
Table passed to humbug
Roses are Red
... back into Lua code
Values in Lua now 527 -511
Table contains ...
hq 404
town 1
hotel 48
[trainee@easterton three]$


Looking around, I have found a number of very much more complex examples of how to embed C in Lua, but nothing as easy as the 1 - 2 - 3 steps I have shown you in this article and the previous one. If you're looking to pass strings (strings in Lua are not null terminated, so differ), to iterate through a Lua table within C, or to modify a table, you'll find that covered in Lua's reference documentation. If you would like to learn more about Lua - from the basics through to the topics covered in this article - and you feel I could help you, please have a look at our public Lua Courses or ask about a private course if you're looking at more specialised needs, or if you have a group of delegates.
(written 2008-10-18 14:49:17)

 
Associated topics are indexed under
C212 - C and C based languages - Memory Management
U117 - Other Lua Subjects

Back to
Calling functions in C from your Lua script - a first HowTo
Previous and next
or
Horse's mouth home
Forward to
Old Piles of the South West
Some other Articles
String matching in Perl with Regular Expressions
30th November - Santa Trip from Melksham
Lua - IAQ (Infrequently Answered Questions)
Old Piles of the South West
Passing a table from Lua into C
Calling functions in C from your Lua script - a first HowTo
How many cups of coffee?
Lua Course, and the Wiltshire Countryside too
Formatting with a leading + / Lua and Perl
Validating Credit Card Numbers
2259 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho