添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
11
9

More than 5 years have passed since last update.

ctypesの変数についてのメモ

Last updated at Posted at 2019-06-22
変数、ポインタの生成と読み書き

ライブラリの読み込み

import ctypes

通常の変数の生成

i = ctypes.c_uint32(10)  # unsigned int i = 10;
f = ctypes.c_float(25.4)  # float = 25.4;

生成した変数の値を取得

i.value  # 10
f.value  # 25.399999618530273

生成した変数への代入

i.value = 20
f.value = 14.2

生成した変数のポインタを取得

i_p = ctypes.pointer(i)
f_p = ctypes.pointer(f)

ポインタから変数を取得

i_p.contents
f_p.contents

ポインタから変数の値を取得

i_p.contents.value  # 20
f_p.contents.value  # 14.199999809265137
配列の生成と読み書き

1次配列の生成

i_arr = (ctypes.c_int32 * 3)()  # int i[3];

2次配列の生成

i_arr = ((ctypes.c_int32 * 4) * 3)()  # int i[3][4];

配列の読み書き

i_arr = (ctypes.c_int32 * 3)()  # int i[3];
i_arr[0] = 10
print(i_arr[0])  # 10
ctypesで扱える型の一覧

https://qiita.com/everylittle/items/6e18ba23f38502c18f3e
https://docs.python.org/ja/3/library/ctypes.html
http://curlnoodle.hatenablog.com/entry/2013/12/30/221858

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9