Roman Mashak said:
> #define BUFLEN 1024
> typedef struct config_s {
> char parameter[BUFLEN];
> void *value;
> } config_t;
> ...
> #define N 10 /* number of parameters in config file */
> config_t conf[N];
> ...
> strcpy(conf[0].parameter, "debug");
> conf[0].value = value;
> ...
>
> It's supposed to keep parameter name and it's value, which can be
> different type (string or unsigned int), that's why I use 'void*' pointer.
Simpler: store a string representation of your int, and just strtol it back
to an int when you need to. Much, much simpler than what you are trying to
do, and commensurately more likely to work.
> But at this point I faced with the problem of type casting. If I call, for
> example:
>
> strcpy(conf[5].parameter, "port");
> conf[PORT].value = (unsigned short)value;
>
> than get compiler's warning:
>
> config.c:61: warning: cast from pointer to integer of different size
> config.c:61: warning: assignment makes pointer from integer without a cast
Right. I can't see any good reason for the (unsigned short) cast. It doesn't
help you in any way. Nor, in fact, would any cast at all. You could do
this:
conf[PORT].value = &value;
and that would compile just fine, but it wouldn't solve your problem;
instead, it would litter your program with pointers to an object that is
almost certainly local in scope - a very bad idea (and no, making it global
isn't the answer either; two wrongs don't make a right).
> I'd wish to keep values of different types in my structure. How is better
> and correct to fulfil this?
If you want to get it working, keep it simple.
#define BUFLEN 1024
#define MAXLEN whatever you think is right
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;
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)