Null Pointers in C: Meaning, Safe Usage, and Common Practices
What is Null Pointers in C: Meaning, Safe Usage, and Common Practices? Every pointer in your program is in one of three states: pointing at valid data, pointing at nothing on purpose, or pointing at something random and dangerous....
What is `NULL`, how does `(void*)0` represent unassigned pointer state, and why must pointers be null-checked before dereferencing?
Explanation & In-Depth Details
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;">#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">stdio</span><span class="js-op">.</span><span class="js-id">h</span><span class="js-op">></span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">(</span><span class="js-kw">void</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">int</span> <span class="js-op">*</span><span class="js-id">p</span> <span class="js-op">=</span> <span class="js-id">NULL</span><span class="js-op">;</span> <span class="js-cm">// p deliberately points at nothing</span>
<span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"p = %p\n"</span><span class="js-op">,</span> <span class="js-op">(</span><span class="js-kw">void</span> <span class="js-op">*)</span><span class="js-id">p</span><span class="js-op">);</span> <span class="js-cm">// typically prints (nil) or 0x0</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">p</span> <span class="js-op">==</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"p points at nothing — safe to handle."</span><span class="js-op">);</span> <span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Sample output:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="" data-language="" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">p</span> <span class="js-op">=</span> <span class="js-op">(</span><span class="js-id">nil</span><span class="js-op">)</span><span class="js-id">p</span> <span class="js-id">points</span> <span class="js-id">at</span> <span class="js-id">nothing</span> — <span class="js-id">safe</span> <span class="js-id">to</span> <span class="js-id">handle</span><span class="js-op">.</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">In practice, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> is almost always <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">((void *)0)</code> — but the point of having a <em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">macro</em> rather than just writing <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">0</code> is that the standard guarantees it works correctly on every platform, including exotic ones where the "null pointer" isn't literally address zero.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Why this matters at all:</span> a dereferenced null pointer crashes <span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">immediately and predictably</span>, at the exact line where the mistake is. That's a <em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">feature</em>. An uninitialized pointer dereference may write into unrelated memory — corrupting data silently, crashing somewhere else entirely, or worse, appearing to work until the demo.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Suggested image: a comparison showing NULL dereference crashing at the exact line vs an uninitialized pointer corrupting memory far away — "the good crash" vs "the bad corruption")</em></p><hr class="my-6" data-streamdown="horizontal-rule" style="border-width: 1px 0px 0px; border-style: solid; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 22.5px 0px 7.5px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; background-color: rgb(18, 18, 22);">The Three Pointer States</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Every pointer needs to be in a known state. Here they are:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-id">x</span> <span class="js-op">=</span> <span class="js-num">42</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-op">*</span><span class="js-id">valid</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">x</span><span class="js-op">;</span> <span class="js-cm">// ✓ points at real data — usableint *nullp = NULL; // ✓ points at nothing — CHECKABLE before useint *uninit; // ✗ points at ??? — unusable and undetectable</span></code></pre></div></div></div><div class="my-4 flex flex-col gap-2 rounded-lg bg-sidebar p-2" data-streamdown="table-wrapper" style="border: 0.8px solid rgb(47, 47, 55); margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 0.5rem; margin-block: 1rem 0.75rem; overflow: hidden; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); gap: 0px; --tw-space-y-reverse: 0; display: flex; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px;"><div class="border-collapse" style="border: 1px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); border-collapse: collapse; border-radius: 5.5px; background-color: rgb(18, 18, 22); max-height: 300px;"><table class="w-full divide-y divide-border" data-streamdown="table" style="border-width: 0px; border-color: inherit; margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); text-indent: 0px; min-width: 100%; width: 956.8px;"><thead class="bg-muted/80" data-streamdown="table-header" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0; background-color: oklab(0.252138 0.000131465 -0.00581433 / 0.8);"><tr class="" data-streamdown="table-row" style="border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);"><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; white-space: nowrap;">State</th><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; white-space: nowrap;">Value</th><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; white-space: nowrap;">Dereference</th><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; white-space: nowrap;">Detectable?</th></tr></thead><tbody class="divide-y divide-border" data-streamdown="table-body" style="border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);"><tr class="" data-streamdown="table-row" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Valid</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Address of real data</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Works</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Yes (<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">!= NULL</code>)</td></tr><tr class="" data-streamdown="table-row" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">NULL</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">0 (typically)</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Crashes</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Yes (<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">== NULL</code>)</td></tr><tr class="" data-streamdown="table-row" style="border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Uninitialized</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Garbage</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Undefined behavior</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">No</span></td></tr></tbody></table></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">The rule that follows:</span> <em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">always</em> put a pointer in one of the two known states. If you don't have a target yet, use <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code>. That single habit turns a silent, delayed, hard-to-locate bug into an immediate, obvious one.</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-op">*</span><span class="js-id">p</span> <span class="js-op">=</span> <span class="js-id">NULL</span><span class="js-op">;</span> <span class="js-cm">// ✓ safe default// ... later, some code path assigns a real target ...if (p != NULL) {</span>
<span class="js-op">*</span><span class="js-id">p</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Suggested image: three boxes labeled with the table above, the uninitialized one marked with a "can't be checked" warning)</em></p><hr class="my-6" data-streamdown="horizontal-rule" style="border-width: 1px 0px 0px; border-style: solid; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 22.5px 0px 7.5px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; background-color: rgb(18, 18, 22);">NULL vs Zero vs '\0'</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Three "nothing" values in C, and they're not interchangeable in meaning:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-op">*</span><span class="js-id">p</span> <span class="js-op">=</span> <span class="js-id">NULL</span><span class="js-op">;</span> <span class="js-cm">// a POINTER that points nowhereint n = 0; // the NUMBER zerochar c = '\0'; // the NULL CHARACTER (also numeric value 0) — string terminator</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> and <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">0</code> and <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">'\0'</code> all have the numeric value zero under the hood, which is why <span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">it's legal to write</span> <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">int *p = 0;</code> — the compiler converts the constant zero to a null pointer. But:</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Write <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> for pointers, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">0</code> for numbers, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">'\0'</code> for the string terminator.</span> The value may be the same; the <em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">meaning</em> isn't, and clarity in C is worth a few characters. A reviewer reading <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">p = 0</code> has to work out your intent; <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">p = NULL</code> says it outright.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">And the one place it matters most</span> — confusing the character <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">'0'</code> with the terminator <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">'\0'</code>:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">char</span> <span class="js-id">digit</span> <span class="js-op">=</span> <span class="js-str">'0'</span><span class="js-op">;</span> <span class="js-cm">// the character zero — a real character, ASCII 48char terminator = '\0'; // the terminator — value 0, ends a string</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Those are <em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">different values</em> (48 vs 0), and mixing them up breaks string handling in ways that look like magic.</p><hr class="my-6" data-streamdown="horizontal-rule" style="border-width: 1px 0px 0px; border-style: solid; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 22.5px 0px 7.5px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; background-color: rgb(18, 18, 22);">Initializing Pointers to NULL</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">The simplest defensive habit, with outsized returns:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-op">*</span><span class="js-id">data</span> <span class="js-op">=</span> <span class="js-id">NULL</span><span class="js-op">;</span> <span class="js-cm">// ✓ declared and immediately in a known state</span>
<span class="js-cm">// ... later in some code path ...data = malloc(sizeof(int) * 10);if (data == NULL) { // allocation failed — handle it}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">What this buys you:</span> if any code path dereferences <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">data</code> before it's properly assigned, you get an immediate crash at that line — not a corruption that surfaces 10,000 instructions later.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">For structs with pointer members</span>, initialize them too:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">typedef</span> <span class="js-id">struct</span> <span class="js-op">{</span> <span class="js-id">int</span> <span class="js-op">*</span><span class="js-id">values</span><span class="js-op">;</span> <span class="js-id">int</span> <span class="js-id">count</span><span class="js-op">;</span> <span class="js-id">char</span> <span class="js-op">*</span><span class="js-id">name</span><span class="js-op">;}</span> <span class="js-id">Container</span><span class="js-op">;</span>
<span class="js-id">Container</span> <span class="js-id">c</span> <span class="js-op">=</span> <span class="js-op">{</span> <span class="js-id">NULL</span><span class="js-op">,</span> <span class="js-num">0</span><span class="js-op">,</span> <span class="js-id">NULL</span> <span class="js-op">};</span> <span class="js-cm">// ✓ all pointers in a known state</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Without that, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">c.values</code> holds whatever was on the stack — and a later <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">if (c.values != NULL)</code> check gives a <span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">false negative</span>, because garbage is very rarely exactly zero. Uninitialized pointer <em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">checks</em> are as dangerous as uninitialized dereferences.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">And after freeing memory:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">free</span><span class="js-op">(</span><span class="js-id">data</span><span class="js-op">);</span>
<span class="js-id">data</span> <span class="js-op">=</span> <span class="js-id">NULL</span><span class="js-op">;</span> <span class="js-cm">// ✓ prevents accidental reuse of a dangling pointer</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">free</code> doesn't set your pointer to <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> — it only releases the memory. The pointer still holds the old address, now pointing at freed memory. Setting it to <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> makes any accidental later use crash immediately instead of corrupting the heap. (And specifically: <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">free(NULL)</code> is guaranteed safe and does nothing — so this pattern makes cleanup code safely re-runnable.)</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Suggested image: the "lifecycle of a pointer" timeline — declared NULL → allocated → used → freed → set NULL again, with the alternative "uninitialized → ???" dangerous path shown crossing it)</em></p><hr class="my-6" data-streamdown="horizontal-rule" style="border-width: 1px 0px 0px; border-style: solid; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 22.5px 0px 7.5px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; background-color: rgb(18, 18, 22);">Checking Pointers Before Use</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">The core defensive discipline: <span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">any pointer you didn't just create and verify should be checked before dereferencing.</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-kw">void</span> <span class="js-id">printValue</span><span class="js-op">(</span><span class="js-kw">const</span> <span class="js-id">int</span> <span class="js-op">*</span><span class="js-id">p</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">p</span> <span class="js-op">==</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"(no value)"</span><span class="js-op">);</span>
<span class="js-kw">return</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Value: %d\n"</span><span class="js-op">,</span> <span class="js-op">*</span><span class="js-id">p</span><span class="js-op">);</span>
<span class="js-op">}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Where NULL checks genuinely matter:</span></p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">1. Function parameters.</span> Any pointer a caller passes could be <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code>:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-id">sumArray</span><span class="js-op">(</span><span class="js-kw">const</span> <span class="js-id">int</span> <span class="js-op">*</span><span class="js-id">arr</span><span class="js-op">,</span> <span class="js-id">int</span> <span class="js-id">n</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">arr</span> <span class="js-op">==</span> <span class="js-id">NULL</span> <span class="js-op">||</span> <span class="js-id">n</span> <span class="js-op"><=</span> <span class="js-num">0</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-cm">// graceful handling of invalid input }</span>
<span class="js-id">int</span> <span class="js-id">total</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op"><</span> <span class="js-id">n</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span> <span class="js-id">total</span> <span class="js-op">+=</span> <span class="js-id">arr</span>[<span class="js-id">i</span>]<span class="js-op">;</span> <span class="js-op">}</span> <span class="js-kw">return</span> <span class="js-id">total</span><span class="js-op">;}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">2. Values returned from functions.</span> Many library functions signal failure with <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code>:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">FILE</span> <span class="js-op">*</span><span class="js-id">fp</span> <span class="js-op">=</span> <span class="js-id">fopen</span><span class="js-op">(</span><span class="js-str">"data.txt"</span><span class="js-op">,</span> <span class="js-str">"r"</span><span class="js-op">);</span><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">fp</span> <span class="js-op">==</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Could not open file: %s\n"</span><span class="js-op">,</span> <span class="js-id">strerror</span><span class="js-op">(</span><span class="js-id">errno</span><span class="js-op">));</span> <span class="js-cm">// errno from <errno.h> return 1;}// use fp...fclose(fp);</span></code></pre></div></div></div><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-op">*</span><span class="js-id">arr</span> <span class="js-op">=</span> <span class="js-id">malloc</span><span class="js-op">(</span><span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">int</span><span class="js-op">)</span> <span class="js-op">*</span> <span class="js-num">100</span><span class="js-op">);</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">arr</span> <span class="js-op">==</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"Out of memory!"</span><span class="js-op">);</span>
<span class="js-kw">return</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">3. Search and lookup results.</span> Functions that might find nothing need a way to say so:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">char</span> <span class="js-op">*</span><span class="js-id">found</span> <span class="js-op">=</span> <span class="js-id">strchr</span><span class="js-op">(</span><span class="js-id">text</span><span class="js-op">,</span> <span class="js-str">'x'</span><span class="js-op">);</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">found</span> <span class="js-op">!=</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Found at index %ld\n"</span><span class="js-op">,</span> <span class="js-id">found</span> <span class="js-op">-</span> <span class="js-id">text</span><span class="js-op">);</span>
<span class="js-op">}</span>
<span class="js-kw">else</span> <span class="js-op">{</span>
<span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"Not found."</span><span class="js-op">);</span>
<span class="js-op">}</span></code></pre></div></div></div><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">char</span> <span class="js-op">*</span><span class="js-id">token</span> <span class="js-op">=</span> <span class="js-id">strtok</span><span class="js-op">(</span><span class="js-id">line</span><span class="js-op">,</span> <span class="js-str">","</span><span class="js-op">);</span>
<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">token</span> <span class="js-op">!=</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-cm">// the standard strtok loop condition printf("%s\n", token);</span>
<span class="js-id">token</span> <span class="js-op">=</span> <span class="js-id">strtok</span><span class="js-op">(</span><span class="js-id">NULL</span><span class="js-op">,</span> <span class="js-str">","</span><span class="js-op">);</span>
<span class="js-op">}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">4. Chained dereferences.</span> Checking only the first pointer isn't enough:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">struct</span> <span class="js-id">Node</span> <span class="js-op">*</span><span class="js-id">node</span> <span class="js-op">=</span> <span class="js-id">getNode</span><span class="js-op">();</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">node</span> <span class="js-op">!=</span> <span class="js-id">NULL</span> <span class="js-op">&&</span> <span class="js-id">node</span><span class="js-op">-></span><span class="js-id">next</span> <span class="js-op">!=</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-cm">// BOTH must be valid printf("%d\n", node->next->value);}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Missing the second check means a crash on the last node — a classic off-by-one at the end of a linked list. <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">&&</code> short-circuits, so the second check is only evaluated when the first passes, which makes this pattern completely safe.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Suggested image: the four check locations as labeled callouts — parameter, return value, search result, chained access)</em></p><hr class="my-6" data-streamdown="horizontal-rule" style="border-width: 1px 0px 0px; border-style: solid; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 22.5px 0px 7.5px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; background-color: rgb(18, 18, 22);">Returning NULL: The Convention for "No Result"</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">C has no exception mechanism and no optional types, so functions that might not produce a result return <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code>. This is a genuine language-wide convention.</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;">#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">stdio</span><span class="js-op">.</span><span class="js-id">h</span><span class="js-op">></span>
<span class="js-id">typedef</span> <span class="js-id">struct</span> <span class="js-op">{</span> <span class="js-id">int</span> <span class="js-id">id</span><span class="js-op">;</span> <span class="js-kw">const</span> <span class="js-id">char</span> <span class="js-op">*</span><span class="js-id">name</span><span class="js-op">;}</span> <span class="js-id">Record</span><span class="js-op">;</span>
<span class="js-id">Record</span> <span class="js-id">records</span>[] <span class="js-op">=</span> <span class="js-op">{</span> <span class="js-op">{</span> <span class="js-num">1</span><span class="js-op">,</span> <span class="js-str">"Alice"</span> <span class="js-op">},</span> <span class="js-op">{</span> <span class="js-num">2</span><span class="js-op">,</span> <span class="js-str">"Bob"</span> <span class="js-op">},</span> <span class="js-op">{</span> <span class="js-num">3</span><span class="js-op">,</span> <span class="js-str">"Carol"</span> <span class="js-op">}};</span><span class="js-id">int</span> <span class="js-id">numRecords</span> <span class="js-op">=</span> <span class="js-num">3</span><span class="js-op">;</span>
<span class="js-cm">// Returns a POINTER to the matching record, or NULL if not foundconst Record *findRecord(int id) { for (int i = 0; i < numRecords; i++) { if (records[i].id == id) { return &records[i]; // found — return its address } } return NULL; // not found}</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">(</span><span class="js-kw">void</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">int</span> <span class="js-id">searchIds</span>[] <span class="js-op">=</span> <span class="js-op">{</span><span class="js-num">2</span><span class="js-op">,</span> <span class="js-num">5</span><span class="js-op">,</span> <span class="js-num">1</span><span class="js-op">};</span>
<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op"><</span> <span class="js-num">3</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span> <span class="js-kw">const</span> <span class="js-id">Record</span> <span class="js-op">*</span><span class="js-id">r</span> <span class="js-op">=</span> <span class="js-id">findRecord</span><span class="js-op">(</span><span class="js-id">searchIds</span>[<span class="js-id">i</span>]<span class="js-op">);</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">r</span> <span class="js-op">!=</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Found ID %d: %s\n"</span><span class="js-op">,</span> <span class="js-id">searchIds</span>[<span class="js-id">i</span>]<span class="js-op">,</span> <span class="js-id">r</span><span class="js-op">-></span><span class="js-id">name</span><span class="js-op">);</span> <span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-op">{</span> <span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"ID %d not found\n"</span><span class="js-op">,</span> <span class="js-id">searchIds</span>[<span class="js-id">i</span>]<span class="js-op">);</span> <span class="js-op">}</span> <span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Sample Run:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="" data-language="" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">Found</span> <span class="js-id">ID</span> <span class="js-num">2</span><span class="js-op">:</span> <span class="js-id">BobID</span> <span class="js-num">5</span> <span class="js-id">not</span> <span class="js-id">foundFound</span> <span class="js-id">ID</span> <span class="js-num">1</span><span class="js-op">:</span> <span class="js-id">Alice</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">The design follows all the guidance from the passing-pointers tutorial: return a pointer (so "found" carries the data and "not found" is unambiguous), use <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">const</code> because the caller shouldn't modify the record, and let the caller decide how to handle the <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> case. This shape mirrors <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">strchr</code>, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">strstr</code>, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">fopen</code>, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">malloc</code> — the whole standard library uses it.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">The rule to remember:</span> when your function has no valid answer to give, return <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> — and document it in the comment above the function, because callers can't tell from the signature alone.</p><hr class="my-6" data-streamdown="horizontal-rule" style="border-width: 1px 0px 0px; border-style: solid; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 22.5px 0px 7.5px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; background-color: rgb(18, 18, 22);">What's Already NULL-Safe (And What Isn't)</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Worth knowing, because it affects how much defensive code you need:</p><div class="my-4 flex flex-col gap-2 rounded-lg bg-sidebar p-2" data-streamdown="table-wrapper" style="border: 0.8px solid rgb(47, 47, 55); margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 0.5rem; margin-block: 1rem 0.75rem; overflow: hidden; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); gap: 0px; --tw-space-y-reverse: 0; display: flex; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px;"><div class="border-collapse" style="border: 1px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); border-collapse: collapse; border-radius: 5.5px; background-color: rgb(18, 18, 22); max-height: 300px;"><table class="w-full divide-y divide-border" data-streamdown="table" style="border-width: 0px; border-color: inherit; margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); text-indent: 0px; min-width: 100%; width: 951.2px;"><thead class="bg-muted/80" data-streamdown="table-header" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0; background-color: oklab(0.252138 0.000131465 -0.00581433 / 0.8);"><tr class="" data-streamdown="table-row" style="border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);"><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; white-space: nowrap;">Function</th><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; white-space: nowrap;">Behavior with NULL</th></tr></thead><tbody class="divide-y divide-border" data-streamdown="table-body" style="border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);"><tr class="" data-streamdown="table-row" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">free(NULL)</code></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">✓ Safe — does nothing</td></tr><tr class="" data-streamdown="table-row" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">puts(NULL)</code></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">✗ Undefined — usually crashes</td></tr><tr class="" data-streamdown="table-row" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">printf("%s", NULL)</code></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">⚠️ Implementation-dependent — glibc prints <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">(null)</code>, but don't rely on it</td></tr><tr class="" data-streamdown="table-row" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">strlen(NULL)</code></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">✗ Crash</td></tr><tr class="" data-streamdown="table-row" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">strcpy(dest, NULL)</code></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">✗ Crash</td></tr><tr class="" data-streamdown="table-row" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">strcmp(NULL, s)</code></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">✗ Crash</td></tr><tr class="" data-streamdown="table-row" style="border-width: 0px 0px 0.8px; border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">memcpy(d, NULL, 0)</code></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">⚠️ Technically UB even with size 0 — avoid</td></tr><tr class="" data-streamdown="table-row" style="border-color: rgb(47, 47, 55); border-image: none; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">fclose(NULL)</code></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">✗ Crash (but <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">fopen</code> failure → <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> → check before <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">fclose</code>)</td></tr></tbody></table></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">The practical takeaway:</span> only <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">free</code> is reliably NULL-tolerant among the common functions. Assume everything else requires a valid pointer, and check.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">That <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">printf("%s", NULL)</code> case is a trap worth flagging: some platforms print <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">(null)</code> and everything looks fine, so the bug hides until you move to a platform where it crashes. Never rely on it — check your pointers.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Suggested image: the table as a safety-reference graphic, with the <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">free(NULL)</code> safe case highlighted)</em></p><hr class="my-6" data-streamdown="horizontal-rule" style="border-width: 1px 0px 0px; border-style: solid; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 22.5px 0px 7.5px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; background-color: rgb(18, 18, 22);">A Complete Example: A Safe Configuration Reader</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">A realistic program that needs NULL discipline throughout:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;">#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">stdio</span><span class="js-op">.</span><span class="js-id">h</span><span class="js-op">></span>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">stdlib</span><span class="js-op">.</span><span class="js-id">h</span><span class="js-op">></span>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">string</span><span class="js-op">.</span><span class="js-id">h</span><span class="js-op">></span>
<span class="js-id">typedef</span> <span class="js-id">struct</span> <span class="js-op">{</span> <span class="js-id">char</span> <span class="js-op">*</span><span class="js-id">host</span><span class="js-op">;</span> <span class="js-id">int</span> <span class="js-id">port</span><span class="js-op">;</span> <span class="js-id">char</span> <span class="js-op">*</span><span class="js-id">user</span><span class="js-op">;}</span> <span class="js-id">Config</span><span class="js-op">;</span>
<span class="js-cm">// Returns a newly allocated, filled config, or NULL on any failureConfig *loadConfig(const char *filename) { if (filename == NULL) { // check the parameter fprintf(stderr, "loadConfig: filename is NULL\n"); return NULL; }</span>
<span class="js-id">FILE</span> <span class="js-op">*</span><span class="js-id">fp</span> <span class="js-op">=</span> <span class="js-id">fopen</span><span class="js-op">(</span><span class="js-id">filename</span><span class="js-op">,</span> <span class="js-str">"r"</span><span class="js-op">);</span> <span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">fp</span> <span class="js-op">==</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-cm">// check the file open perror("loadConfig"); return NULL; }</span>
<span class="js-id">Config</span> <span class="js-op">*</span><span class="js-id">cfg</span> <span class="js-op">=</span> <span class="js-id">malloc</span><span class="js-op">(</span><span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">Config</span><span class="js-op">));</span> <span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">cfg</span> <span class="js-op">==</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-cm">// check the allocation fclose(fp); return NULL; }</span>
<span class="js-cm">// Known-safe defaults first, so cleanup is always valid cfg->host = NULL; cfg->port = 0; cfg->user = NULL;</span>
<span class="js-id">char</span> <span class="js-id">line</span>[<span class="js-num">256</span>]<span class="js-op">;</span>
<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">fgets</span><span class="js-op">(</span><span class="js-id">line</span><span class="js-op">,</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">line</span><span class="js-op">),</span> <span class="js-id">fp</span><span class="js-op">)</span> <span class="js-op">!=</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-cm">// check the read line[strcspn(line, "\n")] = '\0';</span>
<span class="js-id">char</span> <span class="js-op">*</span><span class="js-id">equals</span> <span class="js-op">=</span> <span class="js-id">strchr</span><span class="js-op">(</span><span class="js-id">line</span><span class="js-op">,</span> <span class="js-str">'='</span><span class="js-op">);</span> <span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">equals</span> <span class="js-op">==</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-kw">continue</span><span class="js-op">;</span> <span class="js-cm">// malformed line — skip }</span>
<span class="js-op">*</span><span class="js-id">equals</span> <span class="js-op">=</span> <span class="js-str">'\0'</span><span class="js-op">;</span> <span class="js-id">char</span> <span class="js-op">*</span><span class="js-id">key</span> <span class="js-op">=</span> <span class="js-id">line</span><span class="js-op">;</span> <span class="js-id">char</span> <span class="js-op">*</span><span class="js-id">value</span> <span class="js-op">=</span> <span class="js-id">equals</span> <span class="js-op">+</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">strcmp</span><span class="js-op">(</span><span class="js-id">key</span><span class="js-op">,</span> <span class="js-str">"host"</span><span class="js-op">)</span> <span class="js-op">==</span> <span class="js-num">0</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">free</span><span class="js-op">(</span><span class="js-id">cfg</span><span class="js-op">-></span><span class="js-id">host</span><span class="js-op">);</span> <span class="js-cm">// free any previous value! cfg->host = strdup(value); // POSIX — allocates a copy } else if (strcmp(key, "port") == 0) { cfg->port = atoi(value); } else if (strcmp(key, "user") == 0) { free(cfg->user); cfg->user = strdup(value); } }</span>
<span class="js-id">fclose</span><span class="js-op">(</span><span class="js-id">fp</span><span class="js-op">);</span> <span class="js-kw">return</span> <span class="js-id">cfg</span><span class="js-op">;</span> <span class="js-cm">// caller owns this}</span>
<span class="js-kw">void</span> <span class="js-id">freeConfig</span><span class="js-op">(</span><span class="js-id">Config</span> <span class="js-op">*</span><span class="js-id">cfg</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">cfg</span> <span class="js-op">==</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-cm">// ✓ the polite thing to do return; } free(cfg->host); // free(NULL) is safe — no need to check free(cfg->user); free(cfg);}</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">(</span><span class="js-kw">void</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">Config</span> <span class="js-op">*</span><span class="js-id">cfg</span> <span class="js-op">=</span> <span class="js-id">loadConfig</span><span class="js-op">(</span><span class="js-str">"settings.conf"</span><span class="js-op">);</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">cfg</span> <span class="js-op">==</span> <span class="js-id">NULL</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"Could not load configuration. Using defaults."</span><span class="js-op">);</span> <span class="js-kw">return</span> <span class="js-num">1</span><span class="js-op">;</span> <span class="js-op">}</span>
<span class="js-cm">// Every field might still be NULL — check before using printf("Host : %s\n", cfg->host != NULL ? cfg->host : "(default: localhost)"); printf("Port : %d\n", cfg->port > 0 ? cfg->port : 8080); printf("User : %s\n", cfg->user != NULL ? cfg->user : "(anonymous)");</span>
<span class="js-id">freeConfig</span><span class="js-op">(</span><span class="js-id">cfg</span><span class="js-op">);</span> <span class="js-cm">// hand it back return 0;}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Sample <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">settings.conf</code>:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="" data-language="" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">host</span><span class="js-op">=</span><span class="js-id">example</span><span class="js-op">.</span><span class="js-id">comport</span><span class="js-op">=</span>9090user<span class="js-op">=</span><span class="js-id">admin</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Sample Run:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="" data-language="" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">Host</span> <span class="js-op">:</span> <span class="js-id">example</span><span class="js-op">.</span><span class="js-id">com</span>
<span class="js-id">Port</span> <span class="js-op">:</span> <span class="js-num">9090</span>
<span class="js-id">User</span> <span class="js-op">:</span> <span class="js-id">admin</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">And with a missing file:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="" data-streamdown="code-block" dir="ltr" style="border-width: medium; border-style: none; border-color: currentcolor; border-image: none; margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="" data-language="" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background: 0px 0px; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">loadConfig</span><span class="js-op">:</span> <span class="js-id">No</span> <span class="js-id">such</span> <span class="js-id">file</span> <span class="js-id">or</span> <span class="js-id">directory</span>
<span class="js-id">Could</span> <span class="js-id">not</span> <span class="js-id">load</span> <span class="js-id">configuration</span><span class="js-op">.</span> <span class="js-id">Using</span> <span class="js-id">defaults</span><span class="js-op">.</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Count the NULL checks</span> — parameter validation, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">fopen</code>, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">malloc</code>, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">fgets</code> in a loop, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">strchr</code> for the delimiter, the caller's check on the return, and the ternary checks before printing possibly-NULL fields. That's not paranoia; it's the actual shape of correct C code that talks to files, memory, and the outside world.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Also note the ownership pattern:</span> <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">loadConfig</code> allocates and returns; the caller must call <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">freeConfig</code>. That's the "who allocates, frees" rule — and <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">freeConfig(NULL)</code> being tolerant means the caller doesn't need a redundant check before cleanup. This is exactly how C APIs are designed in practice.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Two notes: <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">strdup</code> is POSIX, not ISO C — if you need strict portability, write a small helper that <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">malloc</code>s and <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">strcpy</code>s. Also, a complete example this long might be better published as a companion "downloaded example" file with the page — something to consider for your site.)</em></p><hr class="my-6" data-streamdown="horizontal-rule" style="border-width: 1px 0px 0px; border-style: solid; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 22.5px 0px 7.5px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; background-color: rgb(18, 18, 22);">Common NULL Mistakes</h2><ol class="list-inside list-decimal whitespace-normal [li_&]:pl-6" data-streamdown="ordered-list" style="border: 0px solid rgb(47, 47, 55); margin: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); list-style: outside decimal; padding-inline-start: 1.375rem; margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Dereferencing without checking</span> — the crash. Any pointer from outside your function should be validated.</li><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Using uninitialized pointers instead of <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code></span> — the check then gives false negatives, since garbage isn't zero. Initialize everything.</li><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Not setting pointers to <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> after <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">free</code></span> — creates a dangling pointer that still passes a <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> check. Free, then null.</li><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Checking one level in a chain</span> — <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">node->next->value</code> needs both <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">node != NULL</code> and <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">node->next != NULL</code>.</li><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Forgetting <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">malloc</code>/<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">fopen</code> return checks</span> — the classic out-of-memory or missing-file crash.</li><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Assuming <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">printf("%s", NULL)</code> is safe</span> — it's implementation-defined; it works on some platforms and crashes on others.</li><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Using <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">0</code> for pointers</span> — legal, but <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> communicates intent. Use <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code>.</li><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Confusing <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">'0'</code> and <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">'\0'</code></span> — a character versus a terminator. Different values, very different results.</li><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Comparing <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> against the wrong thing</span> — <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">if (ptr = NULL)</code> (assignment!) instead of <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">if (ptr == NULL)</code>. This one silently sets your pointer to null and evaluates to false. Compilers warn with <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">-Wall</code>; enable it.</li><li class="py-1 [&>p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Relying on <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> where a length of zero is the real answer</span> — sometimes <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">0</code> results is a legitimate result, not a failure. Design your API's meaning deliberately.</li></ol><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Suggested image: "10 NULL Mistakes" checklist graphic — consistent with the series, with #9 highlighted since it's a silent bug)</em></p><hr class="my-6" data-streamdown="horizontal-rule" style="border-width: 1px 0px 0px; border-style: solid; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 22.5px 0px 7.5px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; background-color: rgb(18, 18, 22);">Final Thoughts</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> is a small idea carrying a large amount of C's safety. It's the pointer's way of saying "nothing here, on purpose" — and because it's checkable, it turns the worst kind of bug (silent memory corruption, crashing elsewhere) into the best kind (an immediate, obvious crash at the right line).</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">The habits worth making automatic: <span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">initialize every pointer to <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> or a real target</span>, <span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">check before dereferencing anything you didn't just create</span>, <span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">return <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> when a function has no answer</span>, <span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">set pointers to <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> after <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">free</code></span>, and <span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">check <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">malloc</code>/<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">fopen</code>/search results</span>. That's five habits, and they eliminate the overwhelming majority of pointer crashes.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: "Inter Variable", sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">One bigger idea worth carrying forward: <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">NULL</code> is C's only built-in way to express "absent." Other languages have <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">Option</code>, <code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: "Geist Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">Maybe</code>, or nullable types the compiler forces you to handle. C just hands you the value and hopes you check. Knowing that this responsibility is entirely yours — and never assuming a pointer is good because it looks like one — is what separates pointer code that works from pointer code that "mostly works."</p><div><br></div>
Code Example
#include <stdio.h>
#include <stdlib.h>
int main() {
int *data = (int*)malloc(sizeof(int) * 100);
// Validate memory allocation
if (data == NULL) {
fprintf(stderr, "Out of memory error!\
");
return 1;
}
data[0] = 42;
printf("Allocated successfully, first element: %d\
", data[0]);
free(data);
data = NULL; // Safe practice: neutralize dangling pointer
return 0;
}Interview Question & Tips
A: The C standard guarantees that in source code, integer `0` or `(void*)0` converts to the platform's null pointer representation. While virtually all modern CPUs use address 0x0, the compiler handles hardware translation.
Q: What happens if you call `free(NULL)` in C?
A: `free(NULL)` is completely safe and does nothing (no-op as per ISO C standard).
Best Practices
This study reference for Null Pointers in C: Meaning, Safe Usage, and Common Practices is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: