Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:16:47+00:00 2026-06-15T08:16:47+00:00

Im trying a simple example of the usage of a generic comparison-function in c

  • 0

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…

#include <stdio.h>
#include <string.h>

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<arrsize; i++)
    {
        for(j=0; j<arrsize; j++)
        {
            if(j!=i)
                dups+=(*comp)(&arr[i], &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 *)=&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;
}

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:

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 *)=&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<10; i++)
     {
         arr2[i]=&arr[i];
     }
     printf("The arr has dups: %d\n", compare(comp, arr2, 10));
     return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:16:48+00:00Added an answer on June 15, 2026 at 8:16 am

    Unfortunately you can’t mix void * and int * like this when they’re part of a function signature. Even though int * can be converted to void *, int (*)(int *, int *) can’t be converted to int (*)(void *, void *). The signatures have to match exactly.

    int
    compareints(void *first, void *second)
    {
        if (*((int *) first) == *((int *) second))
            return 1;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a really simple example to get a first look
I'm just trying to get a simple example of accessing a custom java class
I'm trying to compile a simple example of generic classes / type patterns (see
I am learning and trying simple example using node.js and mongoskin. here is my
I'm studying webrat and cucumber and trying to create simple example. Here is my
I am trying a simple Extension Method example and am unable to increment or
I'm trying to create a simple example of an editable gridview, and for some
I'm trying to do a simple example using jQuerys fadeTo method. It works as
I'm trying to execute a simple example of Multicast sockets on Java. MulticastSocket s
I'm trying to create a simple example to understand how to store then retrieve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.