Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How to declare a structure in Python with ctypes?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJul 15, 2023
UpdatedNov 17, 2026
6 min read

To declare a C structure in Python, subclass ctypes.Structure and list the fields in a _fields_ attribute as (name, type) tuples. ctypes then lays the fields out in memory the way a C compiler would, so you can pass the object to C functions, receive it from them, or read it from a binary buffer.

python

Table of Contents

  • Defining and creating a structure
  • Memory layout, padding and sizeof
  • Nested structures, arrays and bit fields
  • Pointers and self-referencing structures
  • Passing structures to and from C
  • Reading a structure from bytes
  • Frequently Asked Questions
    • Why must I set _fields_ as a list of tuples?
    • How do I print all fields of a structure?
    • What is the difference between Structure and Union?
    • Can I add methods to a structure class?
  • Summary

Defining and creating a structure

Each entry in _fields_ is a pair of a field name and a ctypes type. Fields become attributes of the instance, and the constructor accepts values positionally or by keyword:

python

A c_char array field returns bytes and accepts bytes, so use .encode() and .decode() when you work with str. An instance created with no arguments has every field set to zero, which matches a C memset(&s, 0, sizeof s).

Memory layout, padding and sizeof

ctypes follows the platform ABI, so the compiler's alignment padding appears here too:

python

Each field is a descriptor with .offset and .size, which helps when you compare your definition with a C header. To remove padding, for example for a packed network or file format, set _pack_ before _fields_:

python

_pack_ must be assigned before _fields_, because the layout is fixed when _fields_ is set. Recent Python releases are adding an explicit _layout_ setting for packed structures, so check the ctypes documentation for your version if you target both Windows and Unix layouts.

Nested structures, arrays and bit fields

A field type can be another structure, an array, or a bit field. A third tuple element gives the width in bits:

python

Bit-field packing differs between compilers, so verify the layout against the library you call before you rely on it.

Pointers and self-referencing structures

A structure cannot contain itself, but it can contain a pointer to itself. Declare the class first, then assign _fields_:

python

A null pointer is falsy, so if node.next: is the check for the end of a linked list. Keep the objects that a pointer refers to alive; ctypes does not copy them.

Passing structures to and from C

Pass an instance by value directly, or by reference with ctypes.byref() for a struct * parameter. Declaring argtypes and restype is what makes this reliable. The C library function div returns a div_t structure by value (Linux and macOS):

python

For an output parameter such as void fill(struct Point *out), create an instance and pass ctypes.byref(instance); the function writes into it.

Reading a structure from bytes

from_buffer_copy() builds an instance from existing bytes, and from_buffer() shares memory with a writable buffer. bytes(instance) goes the other way:

python

Byte order follows the machine unless you inherit from ctypes.BigEndianStructure or ctypes.LittleEndianStructure, which is the right choice for network protocols and file formats.

Frequently Asked Questions

Why must I set _fields_ as a list of tuples?

ctypes needs the order, name and type of each field to compute offsets. The order matters because it must match the C declaration exactly.

How do I print all fields of a structure?

Loop over _fields_: for name, *_ in Person._fields_: print(name, getattr(a, name)).

What is the difference between Structure and Union?

In a Structure each field has its own memory, laid out one after another. In a Union all fields start at offset zero and share the same bytes.

Can I add methods to a structure class?

Yes. A Structure subclass is a normal Python class, so you can add methods, properties and __repr__.

Summary

Subclass ctypes.Structure, define _fields_ as (name, type) pairs, and ctypes mirrors the C layout, including padding. Use _pack_ for packed layouts, Point * 4 for arrays, a bit width for bit fields, and POINTER for self-referencing types. Always declare argtypes and restype when passing structures to C, and use sizeof and field offsets to confirm your definition matches the header.

Share