Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The key is to make your compare function take void pointers as well:</p> <pre><code>int comp_function(void *a, void *b){ struct word *word1 = a; struct word *word2 = b; // Use word1 and word2 as before. } </code></pre> <hr> <p>Addendum, concerning <em>why</em> the compiler is giving you warnings:</p> <p>To quote the c99 standard which I found <a href="http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf" rel="nofollow">here</a></p> <blockquote> <p>A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.</p> </blockquote> <p>This means that you can have code like the following, and the compiler won't issue the warning you're seeing:</p> <pre><code>void *a = NULL; int (*f)(int a, char *b) = NULL; a = f; f = a; </code></pre> <p>It's tempting to extrapolate that this means the following will also work (after all, we're just substituting "void*" with "struct foo*", right?)</p> <pre><code>int (*f1)(void*, void*); int (*f2)(struct foo*, struct foo*); f1 = f2; </code></pre> <p>However, this generates your warning since it is <strong>not</strong> trying to assign a pointer type to a pointer to void (or vice-versa) as is allowed by the standard. Instead it is trying to assign a value of type <code>int (*)(struct foo*, struct foo*)</code> to a variable of type <code>int (*)(void*, void*)</code>.</p> <p>Of course, you <em>could</em> try to make the compiler happy with an explicit cast, which convinces the compiler that you must know what you're doing. But in doing so you lose the privilege and safety of getting these warnings even when invoking "iffy" behavior.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload