如何从(void *)转换为其他类型? [英] how to cast from (void*) to other types?

查看:336
本文介绍了如何从(void *)转换为其他类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,全部!


我正在实现功能解析配置文件,如下所示:


#这是评论

port = 10000

path = / var / run / dump.pid

....


声明类型


#define BUFLEN 1024

typedef struct config_s {

char参数[BUFLEN];

void * value;

} config_t;

....

#define N 10 / *参数个数在配置文件中* /

config_t conf [N];

....

strcpy(conf [0] .parameter," debug");

conf [0] .value = value;

....


它应该是保持参数名称及其值,可以是不同的

类型(字符串或无符号整数),这就是我使用''void *''指针的原因。但是在这个

点,我遇到了类型转换的问题。如果我打电话,例如:


strcpy(conf [5] .parameter," port");

conf [PORT] .value =( unsigned short)值;


比得到编译器的警告:


config.c:61:警告:从指针转换为整数不同大小

config.c:61:警告:赋值使得整数指针没有强制转换


我希望保留不同类型的值在我的结构中。怎么样更好

并且更正确吗?


最诚挚的问候,Roman Mashak。电子邮件: mr*@tusur.ru

解决方案




Roman Mashak写道:

大家好!

我正在实现函数解析配置文件,看起来像这样:

#这是评论
port = 10000
path = / var / run / dump.pid
...

宣布类型

#define BUFLEN 1024
typedef struct config_s {
char参数[BUFLEN];
void * value;
} config_t;
...
#define N 10 / *配置文件中的参数数量* /
config_t conf [N];
...
strcpy(conf [0]。参数,debug;;
conf [0] .value = value;
...

它应该保留参数名称,它是'值,可以是不同的类型(字符串或无符号整数),这就是我使用''void *''指针的原因。但是在这个问题上我遇到了类型转换的问题。如果我打电话,例如:

strcpy(conf [5] .parameter," port");
conf [PORT] .value =(unsigned short)value;

比得到编译器的警告:

config.c:61:警告:从指针强制转换为不同大小的整数
config.c:61:警告:赋值从没有投射的整数制作指针


AFAIK,将任何指针类型转换为(void *)不需要进行类型转换。

它会根据你自动为你生成你提供的输入。


在你的情况下,我认为''价值''不是指针类型,因此它的吝啬。

什么是数据类型变量''value''?


你能发一个最能描述问题的最小代码片段。

- Ravi


我希望在我的结构中保留不同类型的值。如何才能更好地实现这一目标?

最诚挚的问候,罗马马沙克。电子邮件: mr*@tusur.ru



< br>

Roman Mashak写道:

大家好!

我正在实现函数解析配置文件,如下所示:

#这是评论
port = 10000
path = / var / run / dump.pid
...

宣告类型

#define BUFLEN 1024
typedef struct config_s {
char参数[BUFLEN];
void * value;
} config_t;




考虑将值存储为char *或char数组而不是void *。

使生活更轻松。当你想使用它时,只需将值转换为适当的类型

,例如

int myval = config_get_int(" port");

const char * path = config_get_string(" path");


Bj?rn

[snip]


blockquote> Roman Mashak说:

#define BUFLEN 1024
typedef struct config_s {
char参数[BUFLEN];
void * value ;
} config_t;
...
#define N 10 / *配置文件中的参数数量* /
config_t conf [N];
...
strcpy(conf [0] .parameter," debug");
conf [0] .value = value;
...

它''应该保留参数名称和它的值,它可以是
不同的类型(字符串或无符号整数),这就是我使用''void *''指针的原因。


更简单:存储int的字符串表示形式,并在需要时将其恢复为
到int。比你想要的更简单得多,并且相应地更有可能工作。

但是在这一点上我遇到了类型转换的问题。如果我打电话,
例如:

strcpy(conf [5] .parameter," port");
conf [PORT] .value =(无符号短)值;

比得到编译器的警告:

config.c:61:警告:从指针强制转换为不同大小的整数
config.c:61 :警告:赋值使得整数指针没有强制转换


对。我看不出(无签名的短片)演员的任何好理由。它不会以任何方式帮助你。事实上,任何演员都不会。你可以这样做:

这个:


conf [PORT] .value =& value;


和那个会编译得很好,但它不能解决你的问题;

而是,它会乱丢你的程序指向一个对象的指针

几乎可以肯定是本地的 - 一个非常糟糕的想法(不,让它全球化

也不是答案;两个错误都不是正确的。)

我''我希望在我的结构中保留不同类型的值。如何才能更好地实现这一目标?




如果你想让它工作,请保持简单。


#define BUFLEN 1024

typedef struct config_s {

char参数[BUFLEN];

int valuetype; / * 0 = str,1 = unsigned int,2 = double,3 =?... * /

char strvalue [MAXLEN];

unsigned int uivalue;

双倍左右;

} config_t;


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


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: mr*@tusur.ru

解决方案



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: mr*@tusur.ru




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]


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)


这篇关于如何从(void *)转换为其他类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆