lua通过一个运行时栈来维护参数传递及返回,使用lua_to*等函数获取lua传递到C函数的参数,使用lua_push*从C函数返回值到lua脚本。此外也可以使用lua_getglobal从C函数获取lua脚本定义的全局变量。

    #include <lua.h>#include <lauxlib.h>#include <stdlib.h> /* For function exit() */#include <stdio.h> /* For input/output */void bail(lua_State *L, char *msg){fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",msg, lua_tostring(L, -1));exit(1);}int lua_func_from_c(lua_State *L){printf("This is C\n");int argc = lua_gettop(L);    /* number of arguments */const char * str = lua_tostring(L,1);    /* the first argument: string */int num = lua_tonumber(L,2); /* the second argument: number */printf("The first argument: %s\n", str);printf("The second argument: %d\n", num);lua_getglobal(L,"global_var");const char * global_str = lua_tostring(L,-1);printf("global_var is %s\n", global_str);int the_second_ret = 2*num;lua_pushstring(L, "the first return");lua_pushnumber(L, the_second_ret);return 2;            /* tell lua how many variables are returned */}int main(int argc, const char *argv[]){if(argc != 2){return 1;}lua_State *L = luaL_newstate();   /* Create new lua state variable *//* Load Lua libraries, otherwise, the lua function in *.lua will be nil */luaL_openlibs(L);/* register new lua function in C */lua_register(L, "lua_func_from_c", lua_func_from_c);if( luaL_loadfile(L,argv[1]) )  /* Only load the lua script file */bail(L, "luaL_loadfile() failed");if( lua_pcall(L,0,0,0) )         /* Run the loaded lua file */bail(L, "lua_pcall() failed");lua_close(L);                  /* Close the lua state variable */    return 0;}

lua脚本(my.lua)如下所示(在lua脚本中,如果变量不用local明确声明为局部变量,则默认为全局变量):

    print("Hello world")global_var = "this is a global string"first, second = lua_func_from_c("the first one", 2)print("the first returned", first)print("the second returned", second)

执行结果:

Hello world
This is C
The first argument: the first one
The second argument: 2
global_var is this is a global string
the first returned the first return
the second returned 4

C函数返回多个参数给lua相关推荐

  1. C:C++ 函数返回多个参数

    C/C++ 函数返回多个参数 转自:https://blog.csdn.net/onlyou2030/article/details/48174461 笔者是 Python 入门的,一直很困惑 C/C ...

  2. Python的函数返回值和参数

    一.函数返回值 *返回多个值 Python的函数返回多值其实就是返回一个tuple.但是,在语法上,返回一个tuple可以省略括号,而多个变量可以同时接收一个tuple,按位置赋给对应的值. *没有返 ...

  3. python函数返回值和参数_python_函数参数和返回值

    01.函数参数和返回值的作用 函数:封装功能独立的代码,在需要时通过函数名调用,可以直接访问全局变量 参数:外界希望在函数内部处理数据 返回值:向外界报告函数的执行结果 定义函数时, 是否接受参数,或 ...

  4. mysql函数输出参数_函数--返回值、参数和作用域

    一.函数的返回值--return的作用 1.return将return后面的值作为函数的输出返回值,当函数被调用时,返回值可以被其他变量接收.使用. 而print只是打印在控制台,一个函数如果没有re ...

  5. C++函数的定义、函数返回值和参数类型、函数重载、重载函数等。

    一.函数 1.何为函数? 能够执行一个功能的可复用的用大括号括起来的代码块: 2.按照函数的拥有者进行分类: 预定义函数:头文件中提供的预定义函数(内置):用户可以直接调用: 自定义函数:用户根据需要 ...

  6. java 函数参数 返回值_java中如何用函数返回值作为post提交的参数?

    1.我想实现的功能是在java程序中导入HttpURLConnection类,然后将函数的值作为post方法要提交的参数,最后显示在显示台上. 2.要用到的函数是自己写的可以显示实时计算机cpu.内存 ...

  7. python中return的理解-Python return语句 函数返回值

    return语句是从python 函数返回一个值,在讲到定义函数的时候有讲过,每个函数都要有一个返回值.Python中的return语句有什么作用,今天就来仔细的讲解一下. python 函数返回值 ...

  8. python return返回值_Python return语句 函数返回值

    return语句是从python 函数返回一个值,在讲到定义函数的时候有讲过,每个函数都要有一个返回值.Python中的return语句有什么作用,今天就来仔细的讲解一下. python 函数返回值 ...

  9. c++函数返回一个数组

    https://www.cnblogs.com/walter-xh/p/6192800.html ---恢复内容开始--- 调用某个函数时经常需要函数返回一个值,我们都知道c++ 的函数返回的是一个c ...

  10. 从顺序表中删除具有最小值的元素(假设唯一)并由函数返回被删除元素的值。

    题目: 从顺序表中删除具有最小值的元素(假设唯一)并由函数返回被删除元素的值.空出的位置由最后一个元素填补,若顺序表为空则显示出错信息并退出运行. 算法思想: 第一步:搜索整个顺序表,查找最小值元素及 ...

最新文章

  1. 深度linux创建微信图标,Deepin Linux 下基于deepin-wine的微信图标不见的问题解决
  2. Redis在Linux上编译
  3. 服务器2003系统文件,win2003图文详解文件服务器的安装步骤
  4. 经济学相关资料20170924.词袋.books
  5. Gitlab+Gerrit+Ldap+nginx+mysql 之Gerrit搭建与配置(一)
  6. apache camel_Apache Camel –从头开始开发应用程序(第1部分/第2部分)
  7. 【存档】MySQL(8.0.12 .msi)安装文档
  8. 一个命令让redis服务端所有信息无所遁形~(收藏吃灰系列)
  9. php如何增加字段,php如何增加字段
  10. c语言程序设计任正云,《C语言程序设计》任正云.ppt
  11. iOS xcode4 编译环境
  12. jquery10发送ajax,使用jquery发送一个ajax请求
  13. 大数据基础(一)——关系+文章
  14. VS如何定制自己的模板信息
  15. python爬取喜马拉雅vip音频_Python简易爬虫教程(三)--爬取喜马拉雅音频
  16. U盘刻录操作系统的详细步骤
  17. STM32应用文件系统--W25Q256(RTT系统)
  18. c语言误差椭圆,平差计算
  19. 基于智能家居万能无线遥控系统设计
  20. [转载]徐文兵:梦与健康

热门文章

  1. k近邻算法_K近邻算法(一)
  2. 动力环境监控系统作用
  3. 计算机财务管理模型的建立步骤,计算机财务管理系统的建立.ppt
  4. 【翻译】PSV初音Miku Project DIVA-f
  5. 抓包工具Wireshark npcap
  6. jszip在线解压压缩文件
  7. 内存映射文件(专门读写大文件)
  8. NETGEAR R7000 更新固件失败 使用TTL-USB修复教程
  9. 最大子段和问题【思路及实现】
  10. 华为云查询弹性云服务器规格信息,查询规格详情和规格扩展信息列表