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.
sizeofEach 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:
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).
sizeofctypes follows the platform ABI, so the compiler's alignment padding appears here too:
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_:
_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.
A field type can be another structure, an array, or a bit field. A third tuple element gives the width in bits:
Bit-field packing differs between compilers, so verify the layout against the library you call before you rely on it.
A structure cannot contain itself, but it can contain a pointer to itself. Declare the class first, then assign _fields_:
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.
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):
For an output parameter such as void fill(struct Point *out), create an instance and pass ctypes.byref(instance); the function writes into it.
from_buffer_copy() builds an instance from existing bytes, and from_buffer() shares memory with a writable buffer. bytes(instance) goes the other way:
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.
_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.
Loop over _fields_: for name, *_ in Person._fields_: print(name, getattr(a, name)).
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.
Yes. A Structure subclass is a normal Python class, so you can add methods, properties and __repr__.
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.