- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to push a Lua table as an argument?
We may want to push the Lua table as an argument to a code written in C++ which uses Lua as an embedded language and for that case we need to make use of the different API functions that the Lua library provides us with.
Example
The Lua code will look something like the code shown below −
a = { numb = 10, create = function(a) print(a); end, increment = function(self) --self.numb = 11; print(self.numb); end, decrement = function(self,i) self.numb = self.numb-i; print(self.numb); end }; b = a;
And the C++ code that will invoke the Lua functions will look like this −
luaL_openlibs(L); luaL_dofile (L,"main.lua"); lua_getglobal(L, "a"); lua_getfield(L, -1, "increment"); lua_pushvalue(L,-2); // get the table a as the argument lua_pcall(L ,1,0,0); printf("
I am done with Lua in C++.
"); lua_close(L);
In the above code, the call to lua_pushvalue(L,-2) is the one that does the magic, as it enables us to pass the table a as the argument.
Output
11 I am done with Lua in C++
- Related Articles
- Alternatives to Lua as an Embedded Language
- How to send an entire array as an argument in C language?
- How to pass an entire structure as an argument to function in C?
- How to pass entire array as an argument to a function in C language?
- How to send individual elements as an argument in C language?
- How to pass Python function as a function argument?
- How to convert JSON string into Lua table?
- How to pass entire structure as an argument to function in C language?
- How to get the number of entries in a Lua table?
- How to pass a dictionary as argument in Python function?
- How do you copy a Lua table by value?
- How to pass the address of structure as an argument to function in C?
- How to remove Lua table entry by its key?
- How can I use a SELECT statement as an argument of MySQL IF() function?
- How to pass individual elements in an array as argument to function in C language?

Advertisements