Skip to content

Instantly share code, notes, and snippets.

@hit9
Last active December 27, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hit9/7244344 to your computer and use it in GitHub Desktop.
Save hit9/7244344 to your computer and use it in GitHub Desktop.
typedef struct post {
char *x;
char *y;
int x_z;
} post_t;
void
foo (post_t *o, char *src)
{
int i=0;
char *p = src;
int len = strlen(src);
for (; *p != '\0'; i++, p++) {
if (*p == 'x') {
o->x = p;
o->x_z = len-i;
}
}
o->y=src;
}
from ctypes import *
foolib = CDLL("./foolib.so")
class Post(Structure):
_fields_ = (
("x", c_void_p),
("y", c_void_p),
("x_z", c_int),
)
o = Post()
s = "iooxooiddfggggggggggggvd"
foolib.foo(byref(o), create_string_buffer(s))
print o.x_z
print string_at(o.x, o.x_z)
print string_at(o.y, len(s))
@hit9
Copy link
Author

hit9 commented Oct 31, 2013

打印结果为何是

foo ➤ python test.py                                                                                                                   
21
ooiddfggggggggggggvd
ooiddfggggggggggggvd

@hit9
Copy link
Author

hit9 commented Oct 31, 2013

不应该 输出下面的吗 :

21
iooxooiddfggggggggggggvd
ooiddfggggggggggggvd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment