Alexis Tevre
Alexis Tevre

Reputation: 11

add 2 void pointer arrays in C

There was the following challenge: Write in C the polymorphic function: void *add(void *arr1, void *arr2);

The function takes 2 arrays of any type and adds their content in a third array, arr3 as:

arr3[0]=arr1[0]+arr2[0];
arr3[1]=arr3[1]+arr2[1];
.....
arr3[n]=arr1[n]+arr2[n];

The function must return the arr3 as the result.

It is not allowed to dereference a void pointer and i don't think that its a good practice to cast the 2 arrays (arr1 and the arr2) into char* because the content could be int, float, double etc. How is it possible to implement a function like this one?

Upvotes: 0

Views: 1109

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

This function declaration

void *add(void *arr1, void *arr2);

does not provide required information about for example the actual type of array elements and the number of elements the arrays contain.

Also you need somewhere to provide a code that will perform the operation of adding two elements of the arrays.

Either you should change the function declaration specifying additional parameters similar to parameters of the standard C function qsort or the required information should be stored somewhere in global variables to which the function will have an access,

Here is a demonstrative program. .

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

size_t nmemb;
size_t size;

void ( *operation )( void *, const void *, const void * );

void * add( const void *arr1, const void *arr2 )
{
    void *result = malloc( nmemb * size );
    
    if ( result )
    {
        for ( size_t i = 0; i < nmemb; i++ )
        {
            operation( ( char * )result + i * size, 
                       ( const char * )arr1 + i * size,
                       ( const char * )arr2 + i * size );
        }
    }
    
    return result;
}

void add_int( void * result, const void *operand1, const void *operand2 )
{
    *( int * )result = *( const int * )operand1 + *( const int * )operand2;
}

void add_double( void * result, const void *operand1, const void *operand2 )
{
    *( double * )result = *( const double * )operand1 + *( const double * )operand2;
}

int main(void) 
{
    int arr1[] = { 0, 1, 2, 3, 4 };
    int arr2[] = { 5, 6, 7, 8, 9 };
    
    nmemb = sizeof( arr1 ) / sizeof( *arr1 );
    size = sizeof( int );
    operation = add_int;
    
    int *result1 = add( arr1, arr2 );
    
    if ( result1 )
    {
        for ( size_t i = 0; i < nmemb; i++ )
        {
            printf( "%d ", result1[i] );
        }
        
        putchar( '\n' );
    }
    
    free( result1 );
    
    double arr3[] = { 0.0, 1.1, 2.2, 3.3, 4.4 };
    double arr4[] = { 5.5, 6.6, 7.7, 8.8, 9.9 };
    
    nmemb = sizeof( arr3 ) / sizeof( *arr3 );
    size = sizeof( double );
    operation = add_double;
    
    double *result2 = add( arr3, arr4 );
    
    if ( result2 )
    {
        for ( size_t i = 0; i < nmemb; i++ )
        {
            printf( "%.1f ", result2[i] );
        }
        
        putchar( '\n' );
    }
    
    free( result2 );
    
    return 0;
}

The program output is

5 7 9 11 13 
5.5 7.7 9.9 12.1 14.3 

Upvotes: 2

Related Questions