Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to cast from (void*) to other types?

Reply
Thread Tools

how to cast from (void*) to other types?

 
 
Ravi Uday
Guest
Posts: n/a
 
      12-19-2005


Roman Mashak wrote:
> Hello, All!
>
> I'm implementing function parsing config file, which look like this:
>
> # this is comment
> port=10000
> path=/var/run/dump.pid
> ...
>
> Declared the type
>
> #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. 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
>


AFAIK, casting any pointer type to (void *) need not be typecasted.
Its automatically casted for you based on the input you provide.

In your case i feel 'value' is NOT a pointer type, hence its cribbing.
Whats the data type of variable 'value' ?

Can you post a minimal code snippet which best describes the problem.


- Ravi


> I'd wish to keep values of different types in my structure. How is better
> and correct to fulfil this?
>
> With best regards, Roman Mashak. E-mail:
>
>


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Bj=F8rn_Augestad?=
Guest
Posts: n/a
 
      12-19-2005
Roman Mashak wrote:
> Hello, All!
>
> I'm implementing function parsing config file, which look like this:
>
> # this is comment
> port=10000
> path=/var/run/dump.pid
> ...
>
> Declared the type
>
> #define BUFLEN 1024
> typedef struct config_s {
> char parameter[BUFLEN];
> void *value;
> } config_t;


Consider storing the value as a char* or char array instead of a void*.
Makes life much easier. Just convert the value to the appropriate type
when you want to use it, e.g.
int myval = config_get_int("port");
const char* path = config_get_string("path");

Bjørn
[snip]
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-19-2005
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)
 
Reply With Quote
 
Roman Mashak
Guest
Posts: n/a
 
      12-19-2005
Hello, All!

I'm implementing function parsing config file, which look like this:

# this is comment
port=10000
path=/var/run/dump.pid
....

Declared the type

#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. 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

I'd wish to keep values of different types in my structure. How is better
and correct to fulfil this?

With best regards, Roman Mashak. E-mail:


 
Reply With Quote
 
Roman Mashak
Guest
Posts: n/a
 
      12-19-2005
Hello, Richard!
You wrote on Mon, 19 Dec 2005 07:09:21 +0000 (UTC):

[skip]
RH> unsigned int uivalue;
RH> double dvalue;
RH> } config_t;
Thanks to everyone for help!

With best regards, Roman Mashak. E-mail:


 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      12-19-2005

>Richard Heathfield wrote:
> 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;
>


Once doing this, why not put the values in a union? This is the
one of the canonical places for one...

-David

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-19-2005
David Resnick said:

> Once doing this, why not put the values in a union? This is the
> one of the canonical places for one...


It depends on his needs, of course. It can be useful to be able to associate
more than one value type with a given name.

--
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)
 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      12-19-2005

Richard Heathfield wrote:
> David Resnick said:
>
> > Once doing this, why not put the values in a union? This is the
> > one of the canonical places for one...

>
> It depends on his needs, of course. It can be useful to be able to associate
> more than one value type with a given name.
>


Yeah, but OP said

"It's supposed to keep parameter name and it's value, which can be
different
type (string or unsigned int)"

While it might be useful to keep multiple types, he didn't ask for it.
Mind you, I prefer your other suggestion, keep everything as strings
then use accessors (config_get_string, config_get_double,
config_get_int,
config_get_bool, etc) to decied what to convert the string to. Neater.

-David

 
Reply With Quote
 
Roman Mashak
Guest
Posts: n/a
 
      12-20-2005
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:


 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      12-20-2005

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:


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57