title: Casting a const void * to a const char * in C
body: <p>So I have a C function that goes like this:</p>
  
  <pre><code>int cmp (const void *a, const void* b)
   return rot13cmp( (const char*)a, (const char*)b );
  }
  </code></pre>
  
  <p>and rot13cmp is another function that takes two parameters of type const char *. </p>
  
  <p>I pass this function into the compare parameter for the C <a href="http://www.tutorialspoint.com/c_standard_library/c_function_qsort.htm" rel="nofollow">qsort</a> function but it doesn't seem to work. </p>
  
  <p>However, if I instead cast the const void * variables by doing </p>
  
  <pre><code>return rot13cmp ( *(const char **)a, *(const char **)b ); 
  </code></pre>
  
  <p>the function then starts to work. I looked this up at SO but every source said that the first method of casting should work so I wanted to know why only the second one worked for me?</p>
  
  <p>Edit: Here's the relevant code I have,</p>
  
  <pre><code>int cmp (const void *a, const void *b) {
   return rot13cmp( (const char *)a, (const char *)b );
  }
  
  int rot13cmp (const char *a, const char *b) {
   while (*a == *b &amp;&amp; *a != '\n') {
    a++;
    b++;
   }
  
   if (*a == *b) { return 0; }
   else if (*a == '\n') { return 1; }
   else if (*b == '\n') { return 1; }
   else { return rot13(*a) - rot13(*b);
  }
  </code></pre>
  
  <p>and rot13 returns an int for the ASCII code of a letter rotated by 13 letters in the alphabet. </p>
  
  <p>I called qsort by doing</p>
  
  <pre><code>qsort(words, word_count, sizeof(char*), cmp);
  </code></pre>
  
  <p>where words is an array of char** and word_count is an int. cmp is also just </p>