Python ctypes:对象没有属性错误
下面是c程序: fqlib.hPython ctypes:对象没有属性错误,python,c,python-3.x,ctypes,Python,C,Python 3.x,Ctypes,下面是c程序: fqlib.h typedef struct queue { int *que; /* the actual array of queue elements */ int head; /* head index in que of the queue */ int count; /* number of elements in queue */ int size; /* max number of el
typedef struct queue {
int *que; /* the actual array of queue elements */
int head; /* head index in que of the queue */
int count; /* number of elements in queue */
int size; /* max number of elements in queue */
} QUEUE;
/*
* the library functions
*/
void qmanage(QUEUE **, int, int); /* create or delete a queue */
void put_on_queue(QUEUE *, int); /* add to queue */
void take_off_queue(QUEUE *, int *); /* pull off queue */
fqlib.c
#include <stdio.h>
#include <stdlib.h>
#include "fqlib.h"
/*
* create or delete a queue
*
* PARAMETERS: QUEUE **qptr space for, or pointer to, queue
* int flag 1 for create, 0 for delete
* int size max elements in queue
*/
void qmanage(QUEUE **qptr, int flag, int size)
{
if (flag){
/* allocate a new queue */
*qptr = malloc(sizeof(QUEUE));
(*qptr)->head = (*qptr)->count = 0;
(*qptr)->que = malloc(size * sizeof(int));
(*qptr)->size = size;
}
else{
/* delete the current queue */
(void) free((*qptr)->que);
(void) free(*qptr);
}
}
// ...
我是否以错误的方式从指向结构的指针获取数据?更正:
从ctypes导入*
so_file=“./fqlib.so”
myq=CDLL(so_文件)
类队列(结构):#不需要使用pass进行前向声明。指针(队列)错误
#_字段的每一侧都有一个下划线(出现错误)
_字段\=[('que',指针(c_int)),#非指针(队列)
(“头”,c_int),
(“计数”,c_int),
(“尺寸”,c_int)]
qmanage=myq.qmanage
qmanage.argtypes=[指针(指针(队列)),c_int,c_int]
qmanage.restype=None#将None用于void。c_void_p是无效的*
q=指针(队列)()
qmanage(byref(q),1,10)#不需要c#u int(1),c#int(10)。argtypes知道。
打印(q.contents.head)
谢谢。实际上是打字错误:)
from ctypes import *
so_file = "./fqlib.so"
myq = CDLL(so_file)
class QUEUE(Structure):
pass
QUEUE.__fields__ = [
('que', POINTER(QUEUE)),
('head', c_int),
('count', c_int),
('size', c_int),
]
qmanage = myq.qmanage
qmanage.argtypes = [POINTER(POINTER(QUEUE)), c_int, c_int]
qmanage.restype = c_void_p
q = POINTER(QUEUE)()
qmanage(byref(q), c_int(1), c_int(10))
print(q.contents.head)