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 8901779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:26:46+00:00 2026-06-15T01:26:46+00:00

I am slowly learning how to program generic functions in C and get into

  • 0

I am slowly learning how to program generic functions in C and get into trouble now and so often. I am making a program that makes a union of two arrays, in this implementation two int arrays. The first problem, which also leads to the second one, is that the compareints (function) does not access one of the passed arguments (void *): I can’t figure out why? I been staring at the screen for to long time now…

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

//makes union of two generic arrays and eliminates duplicates if there are some...
void **
unite(int (*comp)(void *f, void *s), void **first, void **second, int f_size, int s_size,     int bytes, int *x)
{
     int i;
     void **arr=malloc(bytes*(f_size+s_size));
     for(i=0; i<f_size+s_size; i++)
     {
         /* first bigger */
         if(((*comp)(*first, *second))>0)
         {
            *(arr+i)=*(first++);
         }
         /* second bigger */
         else if(((*comp)(*first, *second))<0)
         {
            *(arr+i)=*((second++));
         }
         /* equal => just one copy */
         else
         {
            *(arr+i)=*(first++);
            second++;
         }
     }
     *x=i;
     return arr;
 }

 int
 compareints(void *first, void *second)
 {
     if(*((int *)first)>*((int *)second)) //can't access the memoryloc in second...
         return 1;
     else if(*((int *)first)<*((int *)second))
         return -1;
     else
         return 0;
 }

 int main(int argc, const char * argv[])
 {

     int arr[10]={1, 2, 4, 12, 22, 29, 33, 77, 98};
     int arr2[5]={3, 5, 7, 8, 9};
     void **first=malloc(sizeof(int *)*10);
     void **second=malloc(sizeof(int *)*5);
     //make pointers to static arrays in dynamic arrays
     int f_ind, s_ind;
     for(f_ind=0; f_ind<10; f_ind++)
         first[f_ind]=&arr[f_ind];
     for(s_ind=0; s_ind<5; s_ind++)
         second[s_ind]=&arr2[s_ind];
     int i;
     //make union of the two arrays and print out the result
     void **ret=unite(&compareints, first, second, 10, 5, sizeof(int), &i);
     for(int k=0; k<i; k++)
         printf("%d  ", *((int *)ret[k]));
     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-15T01:26:48+00:00Added an answer on June 15, 2026 at 1:26 am

    I tried an approach thanks to @WhozCraigs post about the index going out of bounds. So I made some small mods and now the program does what it intends to.

    void **
    unite(int (*comp)(void *f, void *s), void **first, void **second, int f_size, int s_size, int bytes, int *x)
    {
        int i;
        int f_ind=0, s_ind=0;
        void **arr=malloc(bytes*(f_size+s_size));
        for(i=0; i<f_size+s_size; i++)
        {
            /* first bigger */
            if(((*comp)(*first, *second))>0)
            {
                s_ind++;
                if(s_ind<s_size)
                    *(arr+i)=*(second++);
                else
                {
                    f_ind++;
                    if(f_ind<f_size)
                        *(arr+i)=*(first++);
                    else
                        break;
                }
            }
             /* second bigger */
            else if(((*comp)(*first, *second))<0)
            {
                f_ind++;
                if(f_ind<f_size)
                    *(arr+i)=*(first++);
                else
                {
                    s_ind++;
                    if(s_ind<s_size)
                        *(arr+i)=*(second++);
                    else
                        break;
                }
            }
            /* equal => just one copy */
            else
            {
                f_ind++;
                s_ind++;
                if(f_ind<f_size && s_ind==s_size)
                {
                    *(arr+i)=*(first++);
                }
                else if(f_ind==f_size && s_ind<s_size)
                {
                    *(arr+i)=*(second++);
                }
                else
                {
                    *(arr+i)=*(first++);
                    second++;
                }  
            }
        }
        *x=i;
        return arr;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am slowly learning how to use callbacks and I am running into trouble.
I'm just now getting into jquery and slowly learning it (shame, I know), and
I'm (slowly) learning Flex 4 and working on skinning a custom component that extends
I am learning Java (slowly) from the ground up, but every now and again
I am learning how to develop for Android slowly. I noticed that when I
I am just learning how to use ScriptingBridges. I made a method that slowly
OK, slowly progressing on learning php and mysql and get stuck on every step.
I'm working on my first application, and I'm slowly learning the objective-c. But now
I'm slowly learning the ins and outs of LINQtoSQL, but this is confusing me.
I'm using Google App Engine and Objectify 3.1 and slowly learning about denormalization and

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.