What's the equivalent of C struct{...} __attribute__((packed))?

Packed Zig struct is a concept that differs from __attribute__((packed)). I think there’s no direct equivalent, but you can achieve same result like this:

struct __attribute__((packed)) foo
{
  char a;
  int c;
  char b;
};

becomes

const foo = extern struct {
    a: u8 align(1),
    c: c_int align(1),
    b: u8 align(1),
};

With alignment of 1 there would be no padding bytes between fields. You can also utilize translate-c feature to construct struct definitions like this from their C counterpart.

Side question, why would you want this?

Welcome to Ziggit btw :grin:

7 Likes