/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <stdint.h>
enum type_v {
UINT8,
UINT16,
UINT32,
UINT64,
FLOAT,
DOUBLE
};
union type_t {
uint8_t* uint8Array;
uint16_t* uint16Array;
uint32_t* uint32Array;
uint64_t* uint64Array;
float* floatArray;
double* doubleArray;
};
double sum(void* voidArray, size_t arrayLength, int arrayType)
{
union type_t arrays;
switch(arrayType) {
case UINT8: arrays.uint8Array = voidArray; break;
case UINT16: arrays.uint16Array = voidArray; break;
case UINT32: arrays.uint32Array = voidArray; break;
case UINT64: arrays.uint64Array = voidArray; break;
case FLOAT: arrays.floatArray = voidArray; break;
case DOUBLE: arrays.doubleArray = voidArray; break;
}
int i;
double result = 0;
for(i = 0; i < arrayLength; i++){
switch(arrayType) {
case UINT8: result += arrays.uint8Array [i]; break;
case UINT16: result += arrays.uint16Array[i]; break;
case UINT32: result += arrays.uint32Array[i]; break;
case UINT64: result += arrays.uint64Array[i]; break;
case FLOAT: result += arrays.floatArray [i]; break;
case DOUBLE: result += arrays.doubleArray[i]; break;
}
}
return result;
}
int main()
{
uint16_t array[] = {1,2,3,4};
double result = sum(array, 4, UINT16);
printf("Sum = %f\n", result);
return 0;
}