title: Why / when to use `intptr_t` for type-casting in C?
body: <p>I have a question regarding using <code>intptr_t</code> vs. <code>long int</code>. I've observed that incrementing memory addresses (e.g. via manual pointer arithmetic) differs by data type. For instance incrementing a char pointer adds 1 to the memory address, whereas incrementing an int pointer adds 4, 8 for a double, 16 for a long double, etc...</p>
  
  <p>At first I did something like this:</p>
  
  <pre><code>char myChar, *pChar;
  float myFloat, *pFloat;
  
  pChar = &amp;myChar;
  pFloat = &amp;myFloat;
  
  printf( "pChar:  %d\n", ( int )pChar );
  printf( "pFloat: %d\n", ( int )pFloat );
  
  pChar++;
  pFloat++;
  
  printf( "and then after incrementing,:\n\n" );
  printf( "pChar:  %d\n", (int)pChar );
  printf( "pFloat:    %d\n", (int)pFloat );
  </code></pre>
  
  <p>which compiled and executed just fine, but XCode gave me warnings for my typecasting: "Cast from pointer to integer of different size."</p>
  
  <p>After some googling and binging (is the latter a word yet?), I saw some people recommend using <code>intptr_t</code>:</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>printf( "pChar:  %ld\n", ( intptr_t )pChar );
  printf( "pFloat: %ld\n", ( intptr_t )pFloat );
  </code></pre>
  
  <p>which indeed resolves the errors. So, I thought, from now on, I should use <code>intptr_t</code> for typecasting pointers... But then after some fidgeting, I found that I could solve the problem by just replacing <code>int</code> with <code>long int</code>:</p>
  
  <pre><code>printf( "pChar:  %ld\n", ( long int )pChar );
  printf( "pFloat: %ld\n", ( long int )pFloat );
  </code></pre>
  
  <p>So my question is, why is <code>intptr_t</code> useful, and when should it used? It seems superfluous in this instance. Clearly, the memory addresses for <code>myChar</code> and <code>myFloat</code> were just too big to fit in an <code>int</code>... so typecasting them to <code>long int</code>s solved the problem. </p>
  
  <p>Is it that sometimes memory addresses are too big for <code>long int</code> as well? Now that I think about it, I guess that's possible if you have > 4GB of RAM, in which case memory addresses could exceed 2^32 - 1 (max value for unsigned long ints...) but C was created long before that was imaginable, right? Or were they that prescient?</p>
  
  <p>Thanks!</p>