我认为您可能缺少的是确切地知道您希望分配结构内存的位置。下面的c代码提供了一个为struct分配内存并返回指向它的指针的函数(new_struct())。#include

#include

#include

typedef struct {

int a;

int b;

} my_struct;

my_struct *new_struct()

{

my_struct *struct_instance = (my_struct *)malloc(sizeof(my_struct));

memset(struct_instance, 0, sizeof(my_struct));

return struct_instance;

}

int modify_struct(my_struct *ms) {

ms->a = 1;

ms->b = 2;

return 0;

}

void print_struct_c(my_struct *ms) {

printf("my_struct {\n"

" a = %d\n"

" b = %d\n"

"}\n", ms->a, ms->b);

}

从Python获取指针,调用执行分配的C函数,然后可以将其传递给将其作为参数的其他C函数。import ctypes

lib_file_path = <<< path to lib file >>>

# Very simple example of how to declare a ctypes structure to twin the

# C library's declaration. This doesn't need to be declared if the Python

# code isn't going to need access to the struct's data members.

class MyStruct(ctypes.Structure):

_fields_ = [('a', ctypes.c_int),

('b', ctypes.c_int)]

def print_struct(s):

# Print struct that was allocated via Python ctypes.

print("my_struct.a = %d, my_struct.b = %d" % (s.a, s.b))

def print_struct_ptr(sptr):

# Print pointer to struct. Note the data members of the pointer are

# accessed via 'contents'.

print("my_struct_ptr.contents.a = %d, my_struct_ptr.contents.b = %d"

% (sptr.contents.a, sptr.contents.b))

my_c_lib = ctypes.cdll.LoadLibrary(lib_file_path)

# If you don't need to access the struct's data members from Python, then

# it's not necessary to declare MyStruct above. Also, in that case,

# 'restype' and 'argtypes' (below) can be set to ctypes.c_void_p instead.

my_c_lib.new_struct.restype = ctypes.POINTER(MyStruct)

my_c_lib.modify_struct.argtypes = [ctypes.POINTER(MyStruct)]

# Call C function to create struct instance.

my_struct_c_ptr = my_c_lib.new_struct()

print_struct_ptr(my_struct_c_ptr)

my_c_lib.modify_struct(my_struct_c_ptr)

print_struct_ptr(my_struct_c_ptr)

# Allocating struct instance from Python, then passing to C function.

my_struct_py = MyStruct(0, 0)

print_struct(my_struct_py)

my_c_lib.modify_struct(ctypes.byref(my_struct_py))

print_struct(my_struct_py)

# Data members of Python allocated struct can be acessed directly.

my_struct_py.a = 555

my_c_lib.print_struct_c(ctypes.byref(my_struct_py)) # Note use of 'byref()'

# to invoke c function.

上面的代码已经更新,包括如何通过Python分配结构实例的示例,以及如何访问C已分配或Python分配结构的数据成员(请注意打印函数中的差异)。

python ctypes 指针_Python Ctypes传递.h文件中定义的结构指针。相关推荐

  1. c语言h文件定义变量,补习C语言之在.h文件中定义全局变量

    话说在一次整理code的时候,无意间将一个原本定义在.c文件中的全局变量移动到.h 文件中(此全局变量被多个.c文件使用),然后突然不能生成.ko(linux中可动态装载的驱动模块).怎么回事?难道是 ...

  2. python echo函数_python如何调用php文件中的函数详解

    前言 python调用php代码实现思路:php文件可通过在terminal中使用php命令行进行调用,因此可使用python开启子进程执行命令行代码.函数所需的参数可通过命令行传递. 测试环境 1. ...

  3. python输入函数格式_python如何提取.c文件中的指定函数的输入参数

    比如你的C文件是c_functions.c: 内容如下:normal_1(in-1, in-2, in-3) { ; } normal_2() { ; } 程序参考源码:import re f = o ...

  4. C++中头文件中定义的变量

    1.在头文件.h中定义static变量,如: static int x;其实就等效于每个引用该头文件的源文件中,定义一个变量名为x的整型静态全局变量,每个文件中的x变量均属于本源文件,各文件中的互不相 ...

  5. python使用logging打印信息到日志文件中

    python使用logging打印信息到日志文件中 参考 Python输出日志信息 Python + logging 输出到屏幕,将log日志写入文件 使用logging打印日志到文件中的目的是: 解 ...

  6. c语言中.h文件中的宏定义,endian.h这个头文件里面的宏可以直接用么?

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 endian.h头文件中以宏的形式定义了大端字节序.小端字节序和混合字节序 #define __LITTLE_ENDIAN 1234 #define __ ...

  7. 使用Python批量筛选上千个Excel文件中的某一行数据并另存为新Excel文件(下篇)

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 野火烧不尽,春风吹又生. 大家好, ...

  8. 在h文件中函定义+ #pragma once 是什么意思?

    文章目录 头文件中能否进行函数的定义 再创一个.cpp 用此预处理指令的作用 编译器在执行一个程序时分以下几步 预处理: 编译 汇编 链接 总结: canci #pragma once 是什么意思? ...

  9. html让文本框左剧中对齐_Python批量设置pptx文件中文本框边框与填充样式(含20分钟视频讲解)...

    近期会议:10月30-11月1日 全国高校Python课程高级研修班(苏州)推荐图书:<Python程序设计(第3版)>,(ISBN:978-7-302-55083-9),董付国,清华大学 ...

最新文章

  1. jenkins配置权限不对导致无法登陆或者空白页面解决办法
  2. maven上解决循环依赖、又不想新加第三模块的方法
  3. JSP标准标签库(JSTL)--简介
  4. Go语言 模糊搜索实验(一)
  5. Git之cherry-pick命令
  6. 四大开源分布式存储_ipfs分布式存储行业面临着四大主要风险,你知道是哪些吗?...
  7. 【算法分析与设计】鸡尾酒排序
  8. Python+pandas绘制平行坐标图
  9. Spring MVC BindingResult异常
  10. epoll哪些触发模式_epoll的内部实现 百万级别句柄监听 lt和et模式非常好的解释...
  11. docker版mysql的使用和配置(2)——docker版mysql的dockerfile
  12. 速修复!开源编辑器CKEditor 中存在两个严重XSS漏洞,影响Drupal 和其它下游应用...
  13. [转载] 蓝胡子国王的金钥匙
  14. 推荐三款windows下提高工作效率的软件(altrun、everything、ditto)
  15. tensorflow dataset 用法 from_tensor_slices dataset.repeat dataset.batch dataset.shuffle
  16. 优雅的解决Springboot:BindingException: Invalid bound statement (not found):异常
  17. #定位系统性能瓶颈# sysdig
  18. 苹果开发者账号注册申请流程
  19. 浏览器窗口、网页尺寸
  20. 点餐系统架构模型_点餐系统的设计与实现.doc

热门文章

  1. 一些有关使用EF的错误用法展开的思考
  2. .net core 集成 sentry 进行异常报警
  3. 一切都要从华为云 CloudIDE 酷似 VS Code 说起
  4. Asp.Net Core Ocelot Consul 微服务
  5. 如何扩展分布式日志组件(Exceptionless)的日志通知?
  6. 远程开发初探 - VS Code Remote Development
  7. Dotnet Core Windows Service
  8. .NET Core项目部署到linux(Centos7)
  9. 开发者的利器:Docker 理解与使用
  10. C# 对程序窗口进程和进程ID