title: Might casting void* into unsigned long int cause undefined behaviour even where sizeof(void*)==sizeof(unsigned long int)
body: <pre><code>size_t size_int = sizeof(unsigned long int);
  size_t size_ptr = sizeof(void*);
  printf(&quot;sizeof(unsigned long int): %zu\n&quot;, size_int);
  printf(&quot;sizeof(void*): %zu\n&quot;, size_ptr);
  
  if(size_int == size_ptr) {
      int a = 0;
      void * ptr_a = &amp;a;
      
      // case 1
      unsigned long int case_1 = *((unsigned long int*)&amp;ptr_a);
      printf(&quot;case 1: %lu\n&quot;, case_1);
  
      // case 2
      unsigned long int case_2 = (unsigned long int)ptr_a;
      printf(&quot;case 2: %lu\n&quot;, case_2);
  
      // case 3
      unsigned long int case_3 = 0;
      memcpy(&amp;case_3, &amp;ptr_a, sizeof(void*));
      printf(&quot;case 3: %lu\n&quot;, case_3);
      
      // case 4
      void *ptr_b = NULL;
      memcpy(&amp;ptr_b, &amp;case_3, sizeof(void*));
      int *ptr_c = (int*)ptr_b;
      *ptr_c = 5;
      printf(&quot;case 5: %i\n&quot;, a);
  }
  </code></pre>
  <p>In fact I am aware that there are uintptr_t and intptr_t in C99. However, for educational purposes I want to ask some questions. Before I start, I know that it is a bad practice and should never be done in this way.</p>
  <p>Q1. Could case 1 cause an undefined behaviour? Is it safe? If it is not, why? If it is safe, is it guaranteed that &quot;case_1&quot; variable holds exactly the same address as an unsigned long int?<br/>
  Q2. Same above for case 2.<br/>
  Q3. Same above for case 3.<br/>
  Q4. Same above for case 4.<br/></p>