C - Polymorphic (void *) arrays

I recently tried to figure out the benefits of using arrays void *instead of typical types ( int, charetc.). So I tried to write a polymorphic function (void *)that takes an array of 3 ( a, b, c) of any type of data and calculates the node between each array aand band puts the results into an array c. The type of calculation (sum, entity, etc.) And the type of data to be calculated depends on the user (maybe we can use two more function arguments).

So, I have a prototype of my function that looks like this:

void *apply(void *a, void *b, void *c, int calc, int typeOfCalc);

I cannot understand how to use the contents of each array void, depending on its type, and how to perform calculations depending on the type of data that the user wants.

EDIT

I found an exercise that asks the same as me and says:

Write a polymorphic function "apply"that takes as parameters a table of size (a, b, c) of elements of any (each) type, makes a transaction between [i] b [i] for each i, and puts the result to act on c [i]. An act will be performed and the result is determined by the function of the caller.

+4
source share
2 answers

typeOfCalc, a, b c. , , "sumFloat". (, ..) (, "operationFloat" ) . :

void sumFloat(void *a, void *b, void *c)
{
    float * f_a = a;
    float * f_b = b;
    float * f_c = c;

    *c = *a + *b;
}

void productFloat(void *a, void *b, void *c)
{
    float * f_a = a;
    float * f_b = b;
    float * f_c = c;

    *c = *a * *b;
}

typedef struct{
    void (*sum)(void *a, void *b, void *c);
    void (*product)(void *a, void *b, void *c);
}Operations;

Operations operationsFloat = {
        .sum = sumFloat,
        .product = productFloat,
};

void apply(void *a, void *b, void *c, char op, Operations * opClass){
    switch (op) {
        case '+':
            opClass->sum(a, b, c);
            break;
        case '*':
            opClass->product(a, b, c);
            break;
        default:
            break;
    }
}
+3

:

#include <stdio.h>

enum OP{SUM, SUB};

void calc_int(const void *pa, const void *pb, void *pc, enum OP op)
{
    switch (op) {
        case SUM:
            *(int *)pc = *(const int *)pa + *(const int *)pb;
            break;
        case SUB:
            *(int *)pc = *(const int *)pa - *(const int *)pb;
            break;
    }
}

void calc_double(const void *pa, const void *pb, void *pc, enum OP op)
{
    switch (op) {
        case SUM:
            *(double *)pc = *(const double *)pa + *(const double *)pb;
            break;
        case SUB:
            *(double *)pc = *(const double *)pa - *(const double *)pb;
            break;
    }
}

void calc(void *pa, void *pb, void *pc, size_t n, size_t size, enum OP op,
          void (cb)(const void *, const void *, void *, enum OP))
{
    char *a = (char *)pa;
    char *b = (char *)pb;
    char *c = (char *)pc;
    size_t i;

    for (i = 0; i < n; i++) {
        cb(a, b, c, op);
        a += size;
        b += size;
        c += size;
    }
}

int main(void)
{
    #define N 3
    int ia[N] = {1, 2, 3};
    int ib[N] = {4, 5, 6};
    int ic[N];
    double da[N] = {4., 5., 6.};
    double db[N] = {1., 2., 3.};
    double dc[N];
    int i;

    calc(ia, ib, ic, N, sizeof(*ia), SUM, calc_int);
    calc(da, db, dc, N, sizeof(*da), SUB, calc_double);

    for (i = 0; i < N; i++) {
        printf("ic[%d] = %d\n", i, ic[i]);
    }
    for (i = 0; i < N; i++) {
        printf("dc[%d] = %f\n", i, dc[i]);
    }   
    return 0;
}
0

All Articles