title: Need help understanding typecasting const void pointer in C
body: <p>I have trouble understanding the first line of code inside <a href="https://github.com/gcc-mirror/gcc/blob/master/libiberty/bsearch.c" rel="nofollow noreferrer">this</a> implementation of the bsearch function in C. I understand the search algorithm itself and I have played around with this function to get a good grasp of it but I still do not get what</p>
  <pre><code>const char *base = (const char *) base0;
  </code></pre>
  <p>does exactly do, why is it permitted, and why does it have to be char and cannot be some other type. When I used the same function but with typecasting to <code>(const int*) base0;</code> instead and then used the <a href="http://pythontutor.com/visualize.html#mode=edit" rel="nofollow noreferrer">C Tutor</a> to understand what's going on I noticed that the variable p becomes a pointer to invalid memory but I have no idea why it happens and why this function works with both strings and integers.</p>