im just came into the question with dynamic type of variable (not really dynamic, but should be determind in the runtime), the situation is like this:
i have a function which accept a double array convert it into the integer and write to a file, the integer can have different Bitlength, like 8, 16 and 32. As it is a array, i want to use a pointer to access the final result (array). So i use the void pointer with malloc and switch case now, but it will be needed to add switch case every where when i trying to access or modify this array, my question is, is there a better way to do this?
current code is like:
void foo(double * arr, int len, int iBits, FILE *fh)
{
void * newArr;
int iBytePerElement, iBase,i;
iBytePerElement = iBits / 8;
iBase = (1 << (iBits - 1)) - 1;
switch (iBytePerElement)
{
case 1:
{
newArr = (int8_t *) malloc(sizeof(int8_t)*len);
break;
}
case 2:
{
newArr = (int16_t *) malloc(sizeof(int16_t)*len);
break;
}
case 4:
{
newArr = (int32_t *) malloc(sizeof(int32_t)*len);
break;
}
}
for (i = 0; i < len; ++i)
{
switch (iBitPerElement)
{
case 1:
{
((int8_t *)newArr)[i] = (int8_t)(arr[i]*iBase);
break;
}
case 2:
{
((int16_t *)newArr)[i] = (int16_t)(arr[i]*iBase);
break;
}
case 4:
{
((int32_t *)newArr)[i] = (int32_t)(arr[i]*iBase);
break;
}
}
}
fwrite(newArr, iBytePerElement, iBytePerElement*len,fh);
}
Making a few assumptions about what you probably meant where your example code is undefined (commented), here's how I might do it:
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
void *foo(double * arr, int len, int iBits) //should return the malloced array not void
{
void * newArr;
int iBytePerElement, iBase, i;
iBytePerElement = iBits / 8;
iBase = (1 << (iBits - 1)) - 1;
if(NULL==(newArr = malloc(iBits/CHAR_BIT))) return NULL; //replace the first switch
//which obviously meant to alloc sizeof(int16_t)*len in the case 2 branch (not (sizeof(int8_t)*len) etc
for (i = 0; i < len; ++i) {
switch(iBytePerElement){
break; case 1: ((int8_t *)newArr)[i] = arr[i]*iBase;
break; case 2: ((int16_t *)newArr)[i] = arr[i]*iBase;
break; case 4: ((int32_t *)newArr)[i] = arr[i]*iBase;
}
}
return newArr;
}
Basically, you only need the second switch.
If you wanted to get rid of the 2nd switch, you could replace it with some function pointer play. For example:
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#define MK_FN(Bits) \
void to##Bits(void *newArr, double const*arr, int len) \
{ \
int i; for(i=0; i < len; i++) ((int##Bits##_t *)newArr)[i] = arr[i]*((1<<(Bits-1)-1)); \
}
MK_FN(8)
MK_FN(16)
MK_FN(32)
void *foo(double * arr, int len, int iBits) //should return the malloced array not void
{
void * newArr;
int iBytePerElement = iBits / 8;
if(NULL==(newArr = malloc(iBits/CHAR_BIT))) return NULL;
((void (*[])(void *,double const*, int)){ [1]=to8, [2]=to16, [4]=to32, })[iBits](newArr,arr,len);
return newArr;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.