title: How to compare C pointers?
body: <p>Recently, I wrote some code to compare pointers like this:</p>
  
  <pre><code>if(p1+len &lt; p2)
  </code></pre>
  
  <p>however, some staff said that I should write like this:</p>
  
  <pre><code>if(p2-p1 &gt; len)
  </code></pre>
  
  <p>to be safe.
  Here,<em>p1</em> and <em>p2</em> are <code>char *</code> pointers,<em>len</em> is an integer.
  I have no idea about that.Is that right?</p>
  
  <p>EDIT1: of course,<em>p1</em> and <em>p2</em> pointer to the same memory object at begging.</p>
  
  <p>EDIT2：just one min ago,I found the <em>bogo</em> of this question in my code(about 3K lines),because <code>len</code> is so big that <code>p1+len</code> can't store in 4 bytes of pointer,so <em>p1+len &lt; p2</em> is <em>true</em>.But it shouldn't in fact,so I think we should compare pointers like this in <em>some situation</em>:</p>
  
  <pre><code>if(p2 &lt; p1 || (uint32_t)p2-p1 &gt; (uint32_t)len)
  </code></pre>