网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python返回函数指针 python指定函数返回值类型

python ctypes 怎么处理函数返回的一般指针

test.c(动态库源代码)

创新互联科技有限公司专业互联网基础服务商,为您提供四川雅安服务器托管高防服务器,成都IDC机房托管,成都主机托管等互联网服务。

[cpp] view plain copy

// 编译生成动态库: gcc -g -fPIC -shared -o libtest.so test.c

#include stdio.h

#include string.h

#include stdlib.h

typedef struct StructPointerTest

{

char name[20];

int age;

}StructPointerTest, *StructPointer;

StructPointer test() // 返回结构体指针

{

StructPointer p = (StructPointer)malloc(sizeof(StructPointerTest));

strcpy(p-name, "Joe");

p-age = 20;

return p;

}

编译:gcc -g -fPIC -shared -o libtest.so test.c

call.py(python调用C语言生成的动态库):

[python] view plain copy

#!/bin/env python

# coding=UTF-8

from ctypes import *

#python中结构体定义

class StructPointer(Structure):

_fields_ = [("name", c_char * 20), ("age", c_int)]

if __name__ == "__main__":

lib = cdll.LoadLibrary("./libtest.so")

lib.test.restype = POINTER(StructPointer)

p = lib.test()

print "%s: %d" %(p.contents.name, p.contents.age)

最后运行结果:

[plain] view plain copy

[zcm@c_py #112]$make clean

rm -f *.o libtest.so

[zcm@c_py #113]$make

gcc -g -fPIC -shared -o libtest.so test.c

[zcm@c_py #114]$./call.py

Joe: 20

[zcm@c_py #115]$

python使用C函数返回的指针

int* GrabImage();

int GetPixel(int* image, int x, int y);

void SetPixel(int* image, int x, int y, int color);

python 中callback怎么使用

在计算机程序设计中,回调函数,或简称回调(Callback),是指通过函数参数传递到其它代码的,某一块可执行代码的引用。这一设计允许了底层代码调用在高层定义的子程序:

例如:

def my_callback(input):

print "function my_callback was called with %s input" % (input,)

def caller(input, func):

func(input)

for i in range(5):

caller(i, my_callback)

执行结果是:

function my_callback was called with 0 input

function my_callback was called with 1 input

function my_callback was called with 2 input

function my_callback was called with 3 input

function my_callback was called with 4 input


网站名称:python返回函数指针 python指定函数返回值类型
URL分享:http://bjjierui.cn/article/dojsejp.html

其他资讯