473,811 Members | 4,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

lvalue cast

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
Nov 15 '05 #1
16 12086
"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.
Nov 15 '05 #2
In article <43************ ***********@new s.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
Nov 15 '05 #3
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
Nov 15 '05 #4
"Martijn" <su************ *********@hot-remove-mail.com> writes:
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
That's an odd warning. The use of cast expressions as lvalues isn't
just deprecated; it's illegal (a constraint violation). It's been
illegal at least since C90; I don't know whether it might have been
allowed before that.

[...] 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*).


I think this should do it:

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

But you might find it cleaner to step through the array using a char*,
and convert the char* value to the appropriate type when you use it.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5
Keith Thompson wrote:
"Martijn" <su************ *********@hot-remove-mail.com> writes:


[snipped]
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*).


I think this should do it:

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


Thanks everyone for the response. The thing that threw me of was +=. I of
course intended to cast just the right hand of the = sign, but using +=
kind-of made that rvalue an lvalue. Now I feel kinda silly askin' this,
especially with the experience I've had programming C ...

[snipped a little more]

Thanks again,

--
Martijn
http://www.sereneconcepts.nl
Nov 15 '05 #6
Martijn wrote:
The thing that threw me of was +=.


(x) += (y)
means exactly
(x) = (x) + (y)
for any values of x and y.

If you have
unsigned short us = USHRT_MAX;
then
++us;
yields undefined behavior, if USHRT_MAX == INT_MAX.

--
pete
Nov 15 '05 #7
In article <11************ *@corp.supernew s.com>,
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote:
"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


That has the distinct disadvantage of invoking undefined behavior if you
use V later (with a few exceptions). The original would hopefully have
defined behavior in the gcc language (which is not quite the same as the
C language).

Consider an implementation where sizeof (ITEMIDLIST *) != sizeof (char
*). Your code is likely to cause trouble. With gcc I would hope that it
does something reasonable in that case.
Nov 15 '05 #8
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
"Martijn" <su************ *********@hot-remove-mail.com> writes:
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


That's an odd warning. The use of cast expressions as lvalues isn't
just deprecated; it's illegal (a constraint violation). It's been
illegal at least since C90; I don't know whether it might have been
allowed before that.


It is legal and deprectated in the language implemented by the gcc
compiler with default settings, which is not quite the same as C.
Nov 15 '05 #9
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
That's an odd warning. The use of cast expressions as lvalues isn't
just deprecated; it's illegal (a constraint violation).
Presumably the gcc extension is deprecated.
illegal at least since C90; I don't know whether it might have been
allowed before that.


It was allowed in some drafts of ANSI C.

-- Richard
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6903
by: danny van elsen | last post by:
hello all, I have recently changed from gcc 3.3.1 to version 3.4.3. In the following code class MyClass { ... private:
13
5040
by: wessoo | last post by:
Hi All. What is The Lvalue Required error message. (What does it mean?Is it an abbreviationof something.) I wrote this test program and I am keeping geting this message. void main() { clrscr();
15
4902
by: Michael Baehr | last post by:
I recently upgraded my Arch Linux system to GCC 3.4, and found out that a previously accepted behavior (cast-as-lvalue) is now marked as deprecated, and will cease to function in GCC 3.5. This has caused several builds to break, most notably elfutils. Presumably this behavior is not part of the C standard and it is thus being excised like many other nonstandard GCC extensions. Regardless, my question is not about the specifics or...
9
13798
by: junky_fellow | last post by:
Consider the following piece of code: (char *)0x100; /* I know that converting an integer to pointer type is implementation defined. But forget this for a moment */ My question is, Why the above expression is not an lvalue ? It both specifies the the storage location as well as the type of object ? When I try to increment it
5
7821
by: A. Farber | last post by:
Hello, I call readv() and writev() in several spots of a program which I run under Linux, OpenBSD and Cygwin. Since it always the same way (check the return value; then check errno and retry if EAGAIN/EINTR), I've written a wrapper function (full source code on the bottom) to call those functions and just pass the function pointer to it: do { ...
3
5771
by: Kavya | last post by:
Can someone give and explain in simple terms a definition of lvalue? Also what are these modifiable and non-modifiable lvalues? I always thought that, if we can assign to anything then that anything is lvalue and if cannot assign to anything then that anything is not lvalue.
0
2094
by: xeee | last post by:
Hi, I can't compile this line of code with gcc. #define PUT_BYTE(p, v) *((unsigned char *)p)++ = (unsigned char )v PUTBYTE(o, 0x58); It gives me the error: ISO C++ forbids cast to non-reference type used as lvalue There's something wrong with the *((unsigned char *)p)++ part.
14
4387
by: nobrow | last post by:
Yes I know what lvalue means, but what I want to ask you guys about is what are all valid lvalues ... a *a a *(a + 1) .... What else?
6
4130
by: Paul Edwards | last post by:
The following code: int main(void) { char *x; (void **)x += 1; return (0); }
0
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10127
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.