Lua C++互传结构体实例
转自: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++互传结构体实例的更多相关文章
- ctypes 操作 python 与 c++ dll 互传结构体指针
CMakeLists.txt # project(工程名) project(blog-3123958139-1) # add_library(链接库名称 SHARED 链接库代码) add_libra ...
- C#调用C++系列二:传结构体
这一篇记录下C#调用C++的结构体的方式来使用OpenCV的数据格式,这里会有两种方式,第一种是C#传一个结构体和图像的路径给C++,然后C++将图像加载进来,再把传进来的结构体填满即可,第二种是C# ...
- C#中使用反射获取结构体实例
一般用反射获取类对象的实例比较简单,只要类有一个无参构造函数或没有显示声明带参的构造函数即可使用如下代码 static void Main(string[] args) { Type type = t ...
- Qt socket中怎么传结构体?
直接发送和接收结构体,例如:struct A {...};struct A objectA; 发送的时候: tcpSocket->write((char *)&objectA, size ...
- C语言结构体实例-创建兔子
参考裸编程思想. #include <stdio.h> //#include "ycjobject.h" // 颜色定义 #define CL_BLACK 0 #def ...
- C结构体中数据的内存对齐问题
转自:http://www.cnblogs.com/qwcbeyond/archive/2012/05/08/2490897.html 32位机一般默认4字节对齐(32位机机器字长4字节),64位机一 ...
- 转载 C#结构体(struct)和类(class)的区别
转载原地址: http://dotnet.9sssd.com/csbase/art/8 C#结构体和类的区别问题:在C#编程语言中,类属于引用类型的数据类型,结构体属于值类型的数据类型,这两种数据类型 ...
- 深入理解C指针之六:指针和结构体
原文:深入理解C指针之六:指针和结构体 C的结构体可以用来表示数据结构的元素,比如链表的节点,指针是把这些元素连接到一起的纽带. 结构体增强了数组等集合的实用性,每个结构体可以包含多个字段.如果不用结 ...
- Swift 结构体的使用
Swift 结构体是构建代码所用的一种通用且灵活的构造体. 我们可以为结构体定义属性(常量.变量)和添加方法,从而扩展结构体的功能. 与 C 和 Objective C 不同的是: 结构体不需要包含实 ...
随机推荐
- IOS工作中的问题(转)
1.UITableView的scrollDelegate问题 下午遇到一个奇怪的问题,之前都没有注意过,由于A VC中要实现tableView和其他View位置的联动,所以实现了tableView的d ...
- 弹出的ViewController半透明效果
在第一个ViewController做如下设置 let controller = UIStoryboard(name: "Main", bundle: nil).instantia ...
- [LeetCode&Python] Problem 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- make: *** No rule to make target `/thread_native.h', needed by `ossl.o'. Stop
修改 Makefile 增加 top_srcdir = ../.. 即可 该文件大多存于ruby源文件下 PS:有时也可能是makefile文件多了空格所致
- HDU 1234:开门人和关门人
开门人和关门人 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- Gym 101745 D.Stamp Stamp Stamp
题目网页链接: http://codeforces.com/gym/101745/problem/D 思路:首先可以确保能够成功染色的字符串都是结果串的子串,那么O(n^2)枚举子串之后dp转移即可. ...
- 习题3.5 求链表的倒数第m个元素(20 分)浙大版《数据结构(第2版)》题目集
请设计时间和空间上都尽可能高效的算法,在不改变链表的前提下,求链式存储的线性表的倒数第m(>0)个元素. 函数接口定义: ElementType Find( List L, int m ); 其 ...
- ssh 免 密码登录另一台机器 和 secureCRT的乱码问题
PS: 就是你把密钥生成好以后,放入B机器中,再登录的时候就已经有了所以就不用验证了 ========================================================= ...
- day8 python学习 集合 深浅拷贝
1.内存地址: 字符串在20位以内,没有空格,没有特殊字符的情况下,同样的字符串内存地址是一样的 2.元组中:在只有一个值的时在后边加逗号和没有逗号的区别 t1=(1) 不加逗号这个值是什么类型就打印 ...
- 解决mysql 8 安装后命令行可以连接,navicat不能连接的问题
错误代码: client does not support authentication 解决办法: 1 使用命令行进入数据库 2 选着数据库 mysql --> user mysql 3 ...