title: printf("%p") and casting to (void *)
body: <p>In a recent question, someone mentioned that when printing a pointer value with printf, the caller must cast the pointer to void *, like so:</p>
  
  <pre><code>int *my_ptr = ....
  
  printf("My pointer is: %p", (void *)my_ptr);
  </code></pre>
  
  <p>For the life of me I can't figure out why. I found <a href="https://stackoverflow.com/questions/7290923/when-printf-is-an-address-of-a-variable-why-use-void">this question</a>, which is almost the same. The answer to question is correct - it explains that ints and pointers are not necessarily the same length.</p>
  
  <p>This is, of course, true, but when I <em>already</em> have a pointer, like in the case above, why should I cast from <code>int *</code> to <code>void *</code>? When is an int * different from a void *? In fact, when does <code>(void *)my_ptr</code> generate any machine code that's different from simply <code>my_ptr</code>? </p>
  
  <p>UPDATE:
  Multiple knowledgeable responders quoted the standard, saying passing the wrong type may result in undefined behavior. How? I expect <code>printf("%p", (int *)ptr)</code> and <code>printf("%p", (void *)ptr)</code> to generate the exact same stack-frame. When will the two calls generate different stack frames?</p>