转自:http://bbs.csdn.net/topics/350261649

=====main.cpp=======
#include "stdio.h" extern "C"
{
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
}; typedef struct
{
int wChairID;
int iHeroID;
int iChosenHeros[];
}
Player; /* LUA接口声明*/
lua_State* L; void Operate(Player &obj)
{
int i; lua_getglobal(L, "PlayOperate"); lua_newtable(L);
lua_pushstring(L, "wChairID");
lua_pushnumber(L, obj.wChairID);
lua_settable(L, -);
lua_pushstring(L, "iHeroID");
lua_pushnumber(L, obj.iHeroID);
lua_settable(L, -); lua_pushstring(L, "iChosenHeros");
lua_newtable(L);
for (i=;i<;++i)
{
lua_pushnumber(L, i);
lua_pushnumber(L, obj.iChosenHeros[i]);
lua_settable(L, -);
}
lua_settable(L, -); lua_call(L, , ); lua_pushstring(L, "wChairID");
int n=lua_gettop(L);
lua_gettable(L, -);
obj.wChairID = (int)lua_tonumber(L, -);
lua_pop(L, );
lua_pushstring(L, "iHeroID");
lua_gettable(L, -);
obj.iHeroID = (int)lua_tonumber(L, -);
lua_pop(L, );
lua_pushstring(L, "iChosenHeros");
lua_gettable(L, -);
for (i=;i<;++i)
{
lua_pushnumber(L, i);
lua_gettable(L, -);
obj.iChosenHeros[i]=(int)lua_tonumber(L, -);
lua_pop(L, );
} } int main(int argc, char *argv[])
{
int i;
Player obj; obj.wChairID = ;
obj.iHeroID = ; for(i=; i<; ++i)
obj.iChosenHeros[i]=; //print initial value
printf( "The origin is blow:\n");
printf( "obj.wChairID = %d\n", obj.wChairID);
printf( "obj.iHeroID = %d\n", obj.iHeroID);
for(i=; i<; ++i)
printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]); /* initialize Lua */
L = lua_open();
if (NULL == L)
{
return -;
}
/* load Lua base libraries */
luaL_openlibs(L); /* load the script */
luaL_dofile(L, "e:\\aaa.lua"); //这里指定aaa.lua文件的位置 /* call function */
Operate(obj); /* print the result */
printf( "The result is blow:\n");
printf( "obj.wChairID = %d\n", obj.wChairID);
printf( "obj.iHeroID = %d\n", obj.iHeroID);
for(i=; i<; ++i)
printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]); /* cleanup Lua */
lua_close(L); return ; } =============aaa.lua==========
function PlayOperate(x)
x.wChairID = x.wChairID+
x.iHeroID = x.iHeroID+
x.iChosenHeros[]=
x.iChosenHeros[]= return x
end

Lua C++互传结构体实例的更多相关文章

  1. ctypes 操作 python 与 c++ dll 互传结构体指针

    CMakeLists.txt # project(工程名) project(blog-3123958139-1) # add_library(链接库名称 SHARED 链接库代码) add_libra ...

  2. C#调用C++系列二:传结构体

    这一篇记录下C#调用C++的结构体的方式来使用OpenCV的数据格式,这里会有两种方式,第一种是C#传一个结构体和图像的路径给C++,然后C++将图像加载进来,再把传进来的结构体填满即可,第二种是C# ...

  3. C#中使用反射获取结构体实例

    一般用反射获取类对象的实例比较简单,只要类有一个无参构造函数或没有显示声明带参的构造函数即可使用如下代码 static void Main(string[] args) { Type type = t ...

  4. Qt socket中怎么传结构体?

    直接发送和接收结构体,例如:struct A {...};struct A objectA; 发送的时候: tcpSocket->write((char *)&objectA, size ...

  5. C语言结构体实例-创建兔子

    参考裸编程思想. #include <stdio.h> //#include "ycjobject.h" // 颜色定义 #define CL_BLACK 0 #def ...

  6. C结构体中数据的内存对齐问题

    转自:http://www.cnblogs.com/qwcbeyond/archive/2012/05/08/2490897.html 32位机一般默认4字节对齐(32位机机器字长4字节),64位机一 ...

  7. 转载 C#结构体(struct)和类(class)的区别

    转载原地址: http://dotnet.9sssd.com/csbase/art/8 C#结构体和类的区别问题:在C#编程语言中,类属于引用类型的数据类型,结构体属于值类型的数据类型,这两种数据类型 ...

  8. 深入理解C指针之六:指针和结构体

    原文:深入理解C指针之六:指针和结构体 C的结构体可以用来表示数据结构的元素,比如链表的节点,指针是把这些元素连接到一起的纽带. 结构体增强了数组等集合的实用性,每个结构体可以包含多个字段.如果不用结 ...

  9. Swift 结构体的使用

    Swift 结构体是构建代码所用的一种通用且灵活的构造体. 我们可以为结构体定义属性(常量.变量)和添加方法,从而扩展结构体的功能. 与 C 和 Objective C 不同的是: 结构体不需要包含实 ...

随机推荐

  1. P1174 互素

    P1174 互素 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 对于某个数n,,我们这次的工作仅是求出小于n且和n互质的数的个数,,比如n=10时 1,3, ...

  2. magento首页点击任何产品都出现404错误的问题方法

    很简单,只要在其他项目上的根目录上复制一个.htaccess文件到项目跟目录下就可以 了,其实就是是设置伪静态:

  3. iOS开发中,如何恢复到某一个版本(Cornerstone)

    Mac上的svn代码管理工具:Cornerstone 如何付恢复某个版本 第一:定位到你的工程,右上角边栏“Working Copy” ---->"Revert" 第二:选择 ...

  4. CodeForces - 778C: Peterson Polyglot (启发式合并trie树)

    Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set ...

  5. 相邻行列相互影响的状态类问题(类似状压dp的搜索)(POJ3279)

    POJ3279http://poj.org/problem?id=3279 题意:黑白的板,每次选择一个十字形翻转(十字板内黑白互换,若是边界则不管),求最小将原图变为全白的策略. 这是一道对于每个格 ...

  6. oracle 11g 建库 建表 增 删 改 查 约束

    一.建库 1.(点击左上角带绿色+号的按钮) 2.(进入这个界面,passowrd为密码.填写完后点击下面一排的Test按钮进行测试,无异常就点击Connect) 二.建表 1-1. create t ...

  7. 【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.1 规则文件

    转载至:https://blog.csdn.net/wo541075754/article/details/75150267 一个标准的规则文件的格式为已“.drl”结尾的文本文件,因此可以通过记事本 ...

  8. ArrayList和LinkedList插入删除效率的测试(完全不在一个数量级8/20)

    通过index获取元素的值 java里面的链表可以添加索引,而C中的链表,是没有索引的 package ArrayListVSLinkedList; import java.util.ArrayLis ...

  9. CentOS升级Python2.6到Python2.7并安装pip

    原文:http://ruter.sundaystart.net/2015/12/03/Update-python/ 貌似CentOS 6.X系统默认安装的Python都是2.6版本的?平时使用以及很多 ...

  10. leetcode:Pascal's Triangle【Python版】

    1.这道题一次提交就AC了: 2.以前用C语言实现的话,初始化二维数组全部为0,然后每行第一个元素为1,只需要用a[i][j] = a[i-1][j]+a[i-1][j-1]就可以了: 3.在Pyth ...