<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Snacks - struct</title>
    <subtitle>Snack-size learning for a fast-paced world.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://snacks.devtestonly.uk/tags/struct/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://snacks.devtestonly.uk"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-10-01T04:20:07+00:00</updated>
    <id>https://snacks.devtestonly.uk/tags/struct/atom.xml</id>
    <entry xml:lang="en">
        <title>C `struct` cheatsheet</title>
        <published>2026-10-01T04:20:07+00:00</published>
        <updated>2026-10-01T04:20:07+00:00</updated>
        
        <author>
          <name>someone</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://snacks.devtestonly.uk/c/struct-cheatsheet/"/>
        <id>https://snacks.devtestonly.uk/c/struct-cheatsheet/</id>
        
        <content type="html" xml:base="https://snacks.devtestonly.uk/c/struct-cheatsheet/">&lt;p&gt;Structures (&lt;code&gt;struct&lt;/code&gt;) in C allow you to group objects of different types together into a single, cohesive data type.&lt;/p&gt;
&lt;h2 id=&quot;1-declaration-and-type-aliasing&quot;&gt;1. Declaration and Type Aliasing&lt;/h2&gt;
&lt;p&gt;A structure type explains the layout but doesn’t allocate memory until an object is defined.&lt;/p&gt;
&lt;h3 id=&quot;basic-declaration&quot;&gt;Basic Declaration&lt;/h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; birdStruct &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; jay&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; magpie&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; raven&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;the-typedef-idiom&quot;&gt;The &lt;code&gt;typedef&lt;/code&gt; Idiom&lt;/h3&gt;
&lt;p&gt;To avoid repeating the &lt;code&gt;struct&lt;/code&gt; keyword, modern C uses &lt;code&gt;typedef&lt;/code&gt; to create type aliases. It is standard practice to use the same tag name:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; birdStruct birdStruct&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; birdStruct &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; jay&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; magpie&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;opaque-structures-forward-declarations&quot;&gt;Opaque Structures (Forward Declarations)&lt;/h3&gt;
&lt;p&gt;You can declare a struct without defining its members to hide implementation details (e.g., in header files). Pointers to opaque structs can be freely passed around.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; toto&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; forward declaration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; toto&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; toto_get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; toto_doit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; toto&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; t&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;2-initialization&quot;&gt;2. Initialization&lt;/h2&gt;
&lt;p&gt;Modern C relies heavily on &lt;strong&gt;designated initializers&lt;/strong&gt; to make code robust and readable.&lt;/p&gt;
&lt;h3 id=&quot;designated-initializers&quot;&gt;Designated Initializers&lt;/h3&gt;
&lt;p&gt;You explicitly name the members you are initializing. The order of members does not matter.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; tm today &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .tm_year &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 2019&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 1900&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .tm_mon  &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .tm_mday &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;default-and-partial-initialization&quot;&gt;Default and Partial Initialization&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Omitted fields:&lt;/strong&gt; Any members omitted in a designated initializer are automatically forced to &lt;code&gt;0&lt;/code&gt; (or &lt;code&gt;nullptr&lt;/code&gt; for pointers).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Default Initializer:&lt;/strong&gt; In C23, &lt;code&gt;{ }&lt;/code&gt; is a valid universal initializer that sets all fields to zero.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; tm unknown_date &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; All members initialized to 0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;3-operations-and-access&quot;&gt;3. Operations and Access&lt;/h2&gt;
&lt;h3 id=&quot;member-access&quot;&gt;Member Access&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;By Value:&lt;/strong&gt; Use the dot operator (&lt;code&gt;.&lt;/code&gt;) to access members of a struct variable.&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;Year: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; today.tm_year&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;By Pointer:&lt;/strong&gt; Use the arrow operator (&lt;code&gt;-&amp;gt;&lt;/code&gt;) to access members via a pointer. &lt;code&gt;a-&amp;gt;tv_sec&lt;/code&gt; is equivalent to &lt;code&gt;(*a).tv_sec&lt;/code&gt;.&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; normalize_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; timespec&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; ts&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;ts&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;        ts&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;tv_sec&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; +=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt; ts&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;tv_nsec&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 1000000000&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;valid-operations&quot;&gt;Valid Operations&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Assignment:&lt;/strong&gt; Structures can be safely assigned to one another using &lt;code&gt;=&lt;/code&gt;.&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; tm tomorrow &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span&gt; today&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function Passing:&lt;/strong&gt; Structs are &lt;strong&gt;passed by value&lt;/strong&gt;. A function receives a full copy of the struct. To modify the original or avoid copying large structs, pass a pointer instead.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;invalid-operations&quot;&gt;Invalid Operations&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Comparison:&lt;/strong&gt; Structures &lt;strong&gt;cannot&lt;/strong&gt; be compared directly using &lt;code&gt;==&lt;/code&gt; or &lt;code&gt;!=&lt;/code&gt;. You must compare them member-by-member.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;4-memory-layout-and-padding&quot;&gt;4. Memory Layout and Padding&lt;/h2&gt;
&lt;p&gt;A struct’s layout is determined by the compiler and is a critical design decision.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Padding:&lt;/strong&gt; The compiler may insert invisible “padding” bytes after any member to satisfy alignment requirements (e.g., aligning a 4-byte &lt;code&gt;int&lt;/code&gt; on a 4-byte memory boundary).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Leading Padding:&lt;/strong&gt; There is &lt;em&gt;never&lt;/em&gt; any padding at the beginning of a struct. A pointer to a struct is guaranteed to point to its first member.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;5-advanced-struct-features&quot;&gt;5. Advanced Struct Features&lt;/h2&gt;
&lt;h3 id=&quot;nested-structures&quot;&gt;Nested Structures&lt;/h3&gt;
&lt;p&gt;A struct can contain other structs. However, in C, nested struct definitions &lt;strong&gt;do not create a new scope&lt;/strong&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; BAD: Stardate&amp;#39;s scope leaks out anyway, making this visually misleading&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; person &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    struct&lt;/span&gt;&lt;span&gt; stardate &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; tm date&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt; bdate&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; GOOD: Declare them flatly on the same level&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; stardate &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; tm date&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; person &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; stardate bdate&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;flexible-array-members-fla&quot;&gt;Flexible Array Members (FLA)&lt;/h3&gt;
&lt;p&gt;An array of unknown size (&lt;code&gt;[]&lt;/code&gt;) can be placed as the &lt;strong&gt;last member&lt;/strong&gt; of a structure to couple an array with its metadata.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; ua32 ua32&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; ua32 &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    size_t&lt;/span&gt;&lt;span&gt; length&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;[]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Flexible array member&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Must dynamically allocate enough memory for the struct + array elements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;size_t&lt;/span&gt;&lt;span&gt; len &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 32&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ua32&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; ap &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; calloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;offsetof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ua32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;uint32_t&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;len&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ap&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;length &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span&gt; len&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;bit-fields-c23-best-practices&quot;&gt;Bit-Fields (C23 Best Practices)&lt;/h3&gt;
&lt;p&gt;You can pack data tightly by specifying the exact number of bits a member requires using &lt;code&gt;: width&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Do not&lt;/strong&gt; use a bare &lt;code&gt;int&lt;/code&gt; for bit-fields.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;_BitInt(N)&lt;/code&gt; for numerical bit-fields of width $N$.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;bool&lt;/code&gt; as the type for a flag bit-field of width 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; tib &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    unsigned&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; _BitInt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; tib_sec : &lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    bool&lt;/span&gt;&lt;span&gt;                tib_isdst : &lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
</feed>
