Re: how to cast from (void*) to other types?
- From: "David Resnick" <lndresnick@xxxxxxxxx>
- Date: 20 Dec 2005 05:32:38 -0800
Roman Mashak wrote:
> Hello, David!
> You wrote on 19 Dec 2005 06:49:20 -0800:
>
> DR> "It's supposed to keep parameter name and it's value, which can be
> DR> different
> DR> type (string or unsigned int)"
>
> DR> While it might be useful to keep multiple types, he didn't ask for it.
> DR> Mind you, I prefer your other suggestion, keep everything as strings
> DR> then use accessors (config_get_string, config_get_double,
> DR> config_get_int,
> DR> config_get_bool, etc) to decied what to convert the string to. Neater.
> The method proposed by RH earlier works fine for me. What about using
> unions, can you give example to let me understand clearly?
>
> With best regards, Roman Mashak. E-mail: mrv@xxxxxxxx
Instead of doing this:
typedef struct config_s {
char parameter[BUFLEN];
int valuetype; /* 0 = str, 1 = unsigned int, 2 = double, 3=?... */
char strvalue[MAXLEN];
unsigned int uivalue;
double dvalue;
} config_t;
You could do this:
typedef struct config_s {
char parameter[BUFLEN];
int valuetype; /* 0 = str, 1 = unsigned int, 2 = double, 3=?... */
union {
char strvalue[MAXLEN];
unsigned int uivalue;
double dvalue;
} value;
} config_t;
Having it be a union has a few virtues
1) saves memory, which usually isn't a big deal, but could be if you
have a union of lots of types
2) clarifies intent. These values are mutually exclusive. You should
have
exactly one of them.
That said, I think it makes rather more sense to have a config
mechanism
where everything is strings and the accessor determines what the type
is...
-David
.
- References:
- how to cast from (void*) to other types?
- From: Roman Mashak
- Re: how to cast from (void*) to other types?
- From: Richard Heathfield
- Re: how to cast from (void*) to other types?
- From: David Resnick
- Re: how to cast from (void*) to other types?
- From: Richard Heathfield
- Re: how to cast from (void*) to other types?
- From: David Resnick
- Re: how to cast from (void*) to other types?
- From: Roman Mashak
- how to cast from (void*) to other types?
- Prev by Date: Re: Problem with printf formats
- Next by Date: Re: Problem with printf formats
- Previous by thread: Re: how to cast from (void*) to other types?
- Next by thread: Re: how to cast from (void*) to other types?
- Index(es):
Relevant Pages
|