473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 12039
"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***********************@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
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_Keith) 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.supernews.com>,
SM Ryan <wy*****@tango-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.org>,
Keith Thompson <ks***@mib.org> 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.org>,
Keith Thompson <ks***@mib.org> 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
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:
# In article <11*************@corp.supernews.com>,
# SM Ryan <wy*****@tango-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.

Unable to divine the exact intention of the programmer, I don't really
know if he wants (*(T*)&V)++ or *(T*)&(V++) or something else, but since
V refers to any lvalue expression rather than just a variable name, I
decided a general wa to cast an lvalue and let him decide how to apply
it in his case.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilarating pointing out the shortcomings of others, is there?
Nov 15 '05 #11
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:

# It is legal and deprectated in the language implemented by the gcc
# compiler with default settings, which is not quite the same as C.

Wrong answer. It's not quite the same as ANSI C, but because ANSI
doesn't have an exclusive legal trademark on the capitialised
third letter of the alphabet, what gcc accepts is still a C dialect.

Lack of precision and bad implicit assumptions make for lousy
programs.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
Nov 15 '05 #12
In article <11*************@corp.supernews.com>,
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:

# It is legal and deprectated in the language implemented by the gcc
# compiler with default settings, which is not quite the same as C.

Wrong answer. It's not quite the same as ANSI C, but because ANSI
doesn't have an exclusive legal trademark on the capitialised
third letter of the alphabet, what gcc accepts is still a C dialect.
Wrong answer. About the first line in the ISO/IEC 9899 Standard says:

"This International Standard specifies the form and establishes the
interpretation of programs written in the C programming language."

There are no dialects of C. There are languages that have some
similarity to C, like the language accepted by the gcc compiler with
default settings, or the C++ language, or the Java language, but they
are not C and they are not dialects of C.
Lack of precision and bad implicit assumptions make for lousy
programs.


I can let that stand.
Nov 15 '05 #13
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:
# In article <11*************@corp.supernews.com>,
# SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
#
# > Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:
# >
# > # It is legal and deprectated in the language implemented by the gcc
# > # compiler with default settings, which is not quite the same as C.
# >
# > Wrong answer. It's not quite the same as ANSI C, but because ANSI
# > doesn't have an exclusive legal trademark on the capitialised
# > third letter of the alphabet, what gcc accepts is still a C dialect.
#
# Wrong answer. About the first line in the ISO/IEC 9899 Standard says:

Doesn't matter what they claim. Unless they have a trademark on "C",
they don't have exclusive definition of the language. It makes as much
sense as Sony claiming only they manufacture portable cassette players.

If you want to see the distinction, look at Ada. DoD did get a
trademark on that language name, so they can talk about _the_ Ada.

# There are no dialects of C. There are languages that have some
# similarity to C, like the language accepted by the gcc compiler with

That will come as a shock to Kernighan and Ritchie, that the language
they were using and writing about for so many years wasn't really C.

# > Lack of precision and bad implicit assumptions make for lousy
# > programs.
#
# I can let that stand.

Try learning it.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
JUSTICE!
Justice is dead.
Nov 15 '05 #14
[[
Stupid deliberately obnoxious '#' quoting character fixed yet
again. Please stop doing that. Whatever point you're trying to
make, if you haven't made it by now, you never will.
]]

SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:
In article <11*************@corp.supernews.com>,
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:

> It is legal and deprectated in the language implemented by the gcc
> compiler with default settings, which is not quite the same as C.

Wrong answer. It's not quite the same as ANSI C, but because ANSI
doesn't have an exclusive legal trademark on the capitialised
third letter of the alphabet, what gcc accepts is still a C dialect.


Wrong answer. About the first line in the ISO/IEC 9899 Standard says:


Doesn't matter what they claim. Unless they have a trademark on "C",
they don't have exclusive definition of the language. It makes as much
sense as Sony claiming only they manufacture portable cassette players.

If you want to see the distinction, look at Ada. DoD did get a
trademark on that language name, so they can talk about _the_ Ada.


The name "Ada" is no longer trademarked.

In this newsgroup, by general consensus, an unqualified reference to
"C" refers to the language as defined by the current ISO standard, or
to the previous one which is still in common use. Historically, it
referred to previous versions of the language from before the first
ANSI standard (the language described in K&R1 or previous versions).
I doubt that K and R themselves would disagree with this.

If you want to be understood, you can either follow common usage or
make it clear that you're talking about something else (e.g., by using
the phrase "GNU C" rather than "C" to refer to a language that
includes the extensions implemented by gcc).

Having a standard is a good thing. Ignoring it, by pretending that
non-standard languages are "C", undermines the whole purpose of having
a standard.
There are no dialects of C. There are languages that have some
similarity to C, like the language accepted by the gcc compiler with


That will come as a shock to Kernighan and Ritchie, that the language
they were using and writing about for so many years wasn't really C.


It was then, because there was no standard yet. The language that
used "=-" as an assignment operator is called C only historically; no
modern correct C compiler will decrement x in response to "x =- 1;".

--
Keith Thompson (The_Other_Keith) 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 #15
Keith Thompson <ks***@mib.org> wrote:

# The name "Ada" is no longer trademarked.

Shows how much I pay attention to DoD.

# In this newsgroup, by general consensus, an unqualified reference to
# "C" refers to the language as defined by the current ISO standard, or

That's not what Christian wrote. He makes a flat out, absolute statement
that ANSI C and only ANSI C is "C" and that all other dialects (which
would have have to include what K+R 1 described) are not "C". That's not
about qualified or unqualified references; he makes an absolute statement
in all contexts which is false.

I'm not part of your consensus. When I wish to be specific, I am specific
and write out ANSI C. It's not hard to type.

# Having a standard is a good thing. Ignoring it, by pretending that
# non-standard languages are "C", undermines the whole purpose of having
# a standard.

ANSI probably has a trademark on "ANSI". They can only claim ownership
of "ANSI C", not "C". Nobody owns "C", and nobody can declare what is
or is not C, only ANSI C, or gnu C, or K+R C, or ...

Now what was it Dijkstra said about mastery of language?

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilarating pointing out the shortcomings of others, is there?
Nov 15 '05 #16
In article <11*************@corp.supernews.com>,
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
Doesn't matter what they claim. Unless they have a trademark on "C",
they don't have exclusive definition of the language.


Even if they had a trademark, it wouldn't give them control of the
truth, only of the use of the name in commercial contexts. The fact
that "Coke" is a trademark doens't make it true that it's "the real
thing", and nothing stops me putting up a sign saying "free Coke" and
giving people Pepsi.

-- Richard
Nov 15 '05 #17

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

Similar topics

6
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
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() {...
15
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...
9
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...
5
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...
3
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...
0
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...
14
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
by: Paul Edwards | last post by:
The following code: int main(void) { char *x; (void **)x += 1; return (0); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.