左倾投射 [英] lvalue cast

查看:66
本文介绍了左倾投射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我最近更新了我的gcc版本(到gcc版本3.4.2

(mingw-special)),我得到了一个警告我不太清楚如何最好

解决:


警告:使用强制转换表达式作为左值不推荐使用


调用此警告的代码与PIDL(指向项目ID

列表的指针,这是一个Windows事物并且实际上并不太重要)有关。问题

是我有一个指向结构的指针(typedef''d到ITEMIDLIST)和

为了访问一些数据我必须移动一定数量字节数(

指针真正指向一个具有任意大小的内存块的数组,并且

结构只是一种接口这些块的方式),所以这就是我b $ b做了:


ITEMIDLIST * pidl;

int offset;


...


(字符*)pidl + =偏移;


如何修复警告?优先不使用额外的

变量(例如char *)。


谢谢,


- -

Martijn
http://www.sereneconcepts.nl

解决方案

" Martijn" <苏********************* @ hot-remove-mail.com>写道:





#我最近更新了我的gcc版本(到gcc版本3.4.2

#(mingw-special)),我收到警告我不太确定如何最好

#solve:



#警告:不推荐使用强制转换表达式使用强制转换表达式



#调用此警告的代码与PIDL(指向项目ID的指针)有关/>
#list,这是一个Windows的东西,并不是真的太重要了)。问题

#是我有一个指向结构的指针(typedef''d到ITEMIDLIST)和

#order来访问一些我需要移动的数据一定数量的字节(

#指针确实指向具有任意大小的内存块的数组,并且

#结构只是一种连接这些块的方法),所以这就是我

#做了:



#ITEMIDLIST * pidl;

#int offset;



#...



#(char *)pidl + = offset;



#我可以做些什么来修复警告?优选地,不使用额外的

#变量(例如char *)。


将类型S的左值V强制转换为类型T的左值,

你可以做类似的事情


*(T *)& V


lvalue S V

指向S& V的指针

左值T *(T *)& V


-

SM瑞恩 http://www.rawbw.com/~wyrmwif/

留给天主教徒来破坏生存。


< blockquote>在文章< 43 *********************** @ news.xs4all.nl>,

Martijn< su *********************@hot-remove-mail.com>写道:

ITEMIDLIST * pidl;
int offset;

...

(char *)pidl + = offset;




你可以做pidl =(ITEMIDLIST *)(((char *)pidl)+ offset)。


其他人可能会解决这个

类的东西的可移植性问题。我假设你知道对齐问题。


- Richard


Martijn写道:




我最近更新了我的gcc版本(到gcc版本3.4.2
(mingw-special)),我收到警告我不太确定如何最好地解决:

警告:不推荐使用强制转换表达式使用强制转换表达式

调用此警告的代码与PIDL(指向项目的指针)有关id
list,这是一个Windows的东西,并不是真的太重要了)。问题
是我有一个指向结构的指针(typedef''d到ITEMIDLIST)并且为了访问一些数据我必须移动一定数量的字节(
指针确实指向具有任意大小的内存块的数组,并且结构只是一种接口这些块的方式),所以这就是我所做的:

ITEMIDLIST * pidl;
int offset;

...

(char *)pidl + = offset;

我该怎么办修复警告?
优先不使用额外的变量(例如char *)。




pidl =(ITEMIDLIST *)((char * )pidl + offset);


-

pete


Hi,

I recently updated my version of gcc (to gcc version 3.4.2
(mingw-special) ), and I get a warning I am not too sure about how to best
solve:

warning: use of cast expressions as lvalues is deprecated

The code that invokes this warning has to do with PIDLs (pointer to item id
list, which are a Windows thing and not too important really). The problem
is that I have a pointer to a structure (typedef''d to ITEMIDLIST) and in
order to access some data I have to move over a certain amount of bytes (the
pointer really points to an array with arbitrary-sized memory blocks, and
the structure is just a way to interface those blocks), so this is what I
did:

ITEMIDLIST* pidl;
int offset;

...

(char*)pidl += offset;

What can I do to fix the warning? Preferebly without the use of an extra
variable (e.g. a char*).

Thanks,

--
Martijn
http://www.sereneconcepts.nl

解决方案

"Martijn" <su*********************@hot-remove-mail.com> wrote:
# Hi,
#
# I recently updated my version of gcc (to gcc version 3.4.2
# (mingw-special) ), and I get a warning I am not too sure about how to best
# solve:
#
# warning: use of cast expressions as lvalues is deprecated
#
# The code that invokes this warning has to do with PIDLs (pointer to item id
# list, which are a Windows thing and not too important really). The problem
# is that I have a pointer to a structure (typedef''d to ITEMIDLIST) and in
# order to access some data I have to move over a certain amount of bytes (the
# pointer really points to an array with arbitrary-sized memory blocks, and
# the structure is just a way to interface those blocks), so this is what I
# did:
#
# ITEMIDLIST* pidl;
# int offset;
#
# ...
#
# (char*)pidl += offset;
#
# What can I do to fix the warning? Preferebly without the use of an extra
# variable (e.g. a char*).

To cast an lvalue V of type S to an lvalue of type T,
you can do something like

*(T*)&V

lvalue S V
pointer to S &V
pointer to T (T*)&V
lvalue T *(T*)&V

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.


In article <43***********************@news.xs4all.nl>,
Martijn <su*********************@hot-remove-mail.com> wrote:

ITEMIDLIST* pidl;
int offset;

...

(char*)pidl += offset;



You could do "pidl = (ITEMIDLIST*)(((char *)pidl) + offset)".

Someone else will probably address thet portability issues with this
sort of thing. I assume you are aware of alignment issues.

-- Richard


Martijn wrote:


Hi,

I recently updated my version of gcc (to gcc version 3.4.2
(mingw-special) ), and I get a warning I am not too sure about how to best
solve:

warning: use of cast expressions as lvalues is deprecated

The code that invokes this warning has to do with PIDLs (pointer to item id
list, which are a Windows thing and not too important really). The problem
is that I have a pointer to a structure (typedef''d to ITEMIDLIST) and in
order to access some data I have to move over a certain amount of bytes (the
pointer really points to an array with arbitrary-sized memory blocks, and
the structure is just a way to interface those blocks), so this is what I
did:

ITEMIDLIST* pidl;
int offset;

...

(char*)pidl += offset;

What can I do to fix the warning?
Preferebly without the use of an extra variable (e.g. a char*).



pidl = (ITEMIDLIST *)((char *)pidl + offset);

--
pete


这篇关于左倾投射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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