Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to initialize generic functions in c
    primarykey
    data
    text
    <p>Im trying a simple example of the usage of a generic comparison-function in c but when initializing it I get the warning message that the types are incompatible when initializing them. Feels as I am missing something basic here...</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; int compare(int (*comp)(void *first, void *second), void *arr, int size) { int i, j, dups=0, arrsize=sizeof(arr)/size; for(i=0; i&lt;arrsize; i++) { for(j=0; j&lt;arrsize; j++) { if(j!=i) dups+=(*comp)(&amp;arr[i], &amp;arr[j]); } } return dups; } int compareints(int *first, int *second) { if(*first==*second) return 1; return 0; } int main(int argc, const char * argv[]) { int (*comp)(void *, void *)=&amp;compareints; //Here is where I get the warning... int arr[10]={22, 39, 78, 22, 99, 12, 82, 10, 11, 28}; printf("The arr has dups: %d\n", compare(comp, arr, sizeof(int))); return 0; } </code></pre> <p>EDIT: the program was not functioning properly and it had to do with the handling of the array. The modifications for functioning prog are below:</p> <pre><code>int compare(int (*comp)(void *first, void *second), void **arr, int size) .... .... .... dups+=(*comp)(arr[i], arr[j]); int compareints(....) if((*((int *)first))==(*((int *)second))) return 1; int main(int argc, const char * argv[]) { int (*comp)(void *, void *)=&amp;compareints; int arr[10]={22, 39, 78, 22, 99, 12, 82, 10, 11, 28}; void **arr2=malloc(sizeof(int *)*10); for(int i=0; i&lt;10; i++) { arr2[i]=&amp;arr[i]; } printf("The arr has dups: %d\n", compare(comp, arr2, 10)); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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