title: C null pointer arithmetic
body: <p>I noticed this warning from Clang:</p>
  <pre><code>warning: performing pointer arithmetic on a null pointer
  has undefined behavior [-Wnull-pointer-arithmetic]
  </code></pre>
  <p>In details, it is this code which triggers this warning:</p>
  <pre><code>int *start = ((int*)0);
  int *end = ((int*)0) + count;
  </code></pre>
  <p>The constant literal zero converted to any pointer type decays into the null pointer constant, which does not point to any contiguous area of memory but still has the type <em>pointer to type</em> needed to do pointer arithmetic.</p>
  <p>Why would arithmetic on a null pointer be forbidden when doing the same on a non-null pointer obtained from an integer different than zero does not trigger any warning?</p>
  <p>And more importantly, does the C standard <strong>explicitly forbid null pointer arithmetic</strong>?</p>
  <hr />
  <p>Also, this code will not trigger the warning, but this is because the pointer is not evaluated at compile time:</p>
  <pre><code>int *start = ((int*)0);
  int *end = start + count;
  </code></pre>
  <p>But a good way of avoiding the undefined behavior is to explicitly cast an integer value to the pointer:</p>
  <pre><code>int *end = (int *)(sizeof(int) * count);
  </code></pre>