Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

Illustration - Delegates on a private Lua course. Although we'll run private courses for larger groups, we limit public courses to just eight delegates so that we can provide good coverage and support for the wide variety of applications to which Lua is put
(written 2008-10-18, updated 2010-06-19)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
U117 - Other Lua Subjects
  [1844] Calling functions in C from your Lua script - a first HowTo - (2008-10-17)
  [2461] Luac - getting lua to start fast by precompiling - (2009-10-20)

C212 - C and C based languages - Memory Management
  [1497] Training Season Starts again! - (2008-01-07)
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)
  [1589] Dynamic Memory Allocation in C - calloc, realloc - (2008-03-22)
  [1670] Dynamic Memory Allocation in C - (2008-06-09)
  [2669] Efficient use of dynamic memory - C and realloc - (2010-03-10)
  [2848] C course - final course example puts it all together - (2010-07-02)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [3416] Storing Tcl source code encoded, and running via your own C program - (2011-09-02)
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [4340] Simple C structs - building up to full, dynamic example - (2014-12-03)
  [4634] Regression testing - via a very short C testing framework - (2016-01-29)
  [4635] Encapsulating logic in functions and structs - the C approach to Object Oriented techniques - (2016-01-30)


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
How many cups of coffee?
Lua Course, and the Wiltshire Countryside too
Formatting with a leading + / Lua and Perl
Validating Credit Card Numbers
4759 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, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/1845_Pas ... nto-C.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb