P
pozz
In my software, there are some variables that represents some settings.
They usually are numerical variables: unsigned char (0..255),
signed char (-127..128), unsigned int (0..65535), signed int (-32767..32768).
I want to write a generic C function that changes the value of one parameter.
For example, a function that takes a pointer to the variable and increments
it by one. Of course, the function doesn't know the variable type (unsigned
char, signed char, and so on).
I thought to solve this problem in two ways, but I don't like them.
The first solution is to create a struct with 4 different pointers where
only one of that is not NULL.
struct {
unsigned char *p_uchar;
signed char *p_schar;
unsigned int *p_uint;
signed int *p_sint;
...
} param;
But I will lost many RAM space and the incrementing function will be poor
optimized.
if( p_uchar )
*p_uchar++;
else if( p_schar )
*p_schar++;
else if( p_uint )
*p_uint++;
else if( p_sint )
*p_sint++;
The other solution is to use a member of the struct to identify the type of
the variable and using void pointer for the variable.
#define PARAMTYPE_UCHAR 1
#define PARAMTYPE_SCHAR 2
#define PARAMTYPE_UINT 3
#define PARAMTYPE_SINT 4
struct {
int type;
void *p_value;
...
};
In this way I will allocate less space than before but the incrementing function
is low optimized.
if( type==PARAMTYPE_UCHAR )
(*(unsigned char *)p_value)++;
else if( type==PARAMTYPE_SCHAR )
(*(signed char *)p_value)++;
....
I wonder that a different and more elegant solution there is.
I think to the printf() function. I pass int and char with %d parameter
and the printf() works well, but it doesn't know the exact type of
the variable.
Maybe the only problem is with unsigned numerical variables that want
the %u parameter, I think.
They usually are numerical variables: unsigned char (0..255),
signed char (-127..128), unsigned int (0..65535), signed int (-32767..32768).
I want to write a generic C function that changes the value of one parameter.
For example, a function that takes a pointer to the variable and increments
it by one. Of course, the function doesn't know the variable type (unsigned
char, signed char, and so on).
I thought to solve this problem in two ways, but I don't like them.
The first solution is to create a struct with 4 different pointers where
only one of that is not NULL.
struct {
unsigned char *p_uchar;
signed char *p_schar;
unsigned int *p_uint;
signed int *p_sint;
...
} param;
But I will lost many RAM space and the incrementing function will be poor
optimized.
if( p_uchar )
*p_uchar++;
else if( p_schar )
*p_schar++;
else if( p_uint )
*p_uint++;
else if( p_sint )
*p_sint++;
The other solution is to use a member of the struct to identify the type of
the variable and using void pointer for the variable.
#define PARAMTYPE_UCHAR 1
#define PARAMTYPE_SCHAR 2
#define PARAMTYPE_UINT 3
#define PARAMTYPE_SINT 4
struct {
int type;
void *p_value;
...
};
In this way I will allocate less space than before but the incrementing function
is low optimized.
if( type==PARAMTYPE_UCHAR )
(*(unsigned char *)p_value)++;
else if( type==PARAMTYPE_SCHAR )
(*(signed char *)p_value)++;
....
I wonder that a different and more elegant solution there is.
I think to the printf() function. I pass int and char with %d parameter
and the printf() works well, but it doesn't know the exact type of
the variable.
Maybe the only problem is with unsigned numerical variables that want
the %u parameter, I think.