gcc: What's illegal about this ?

C

Chris Dollin

Richard said:
(e-mail address removed) wrote...

In the expression `E++`, `E` must be an lvalue -- a location,
a place to put something, something that could appear as the
target of an assignment statement.

`(T) E`, a cast, isn't an lvalue, etc. So it can't appear as
the operand of `++` (neither prefix nor postfix).

Some gcc's with some command line options set some way don't
bother to tell you about this and generate the "obvious" code
for those cases where there /is/ some "obvious" code.

[I haven't seen the other responses.]

--
"Some of these", Hazleton had said, looking at a /A Clash of Cymbals/
just-completed tangle of wires, lenses, antennae and
kernels of metal with rueful respect, "ought to prove
pretty potent in the pinch. I just wish I knew which
ones they were".

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
C

CBFalconer

Richard said:
.... snip ...

Seems like the problem is the cast. I'm sure at least one of the
responses explains why. To me, it's done to make sure I'm adding
to sum only short-sized values and I'm utterly clueless as to why
the compiler wouldn't allow it.

Because the result of the cast is an expression, not an object.
There is no place to keep the incremented value of that expression.
 
B

Ben Bacarisse

Richard Eich said:
(e-mail address removed) wrote...

Of course. Sorry.

iphp is a pointer to an IPv4 header. Specifically, it holds the
address of a populated IPv4 header ('iph' below).

sum is a long. It holds checksum calculation for the IPv4 header.

...
struct ip_hdr * iphp = &iph ;

There is really no need use this hack here. If it is safe and correct
to access iph in "short" sized chunks, then just convert the pointer
once rather than every time:

unsigned short *iphp = (unsigned short)&iph;
int i, hlc = sizeof(iph) ;
long sum ;

for ( i = 0, sum = 0; hlc > 1; i++ )
{
sum += *((unsigned short *)iphp)++ ;

No cast now needed, just 'sum += *iphp++;'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,688
Members
48,796
Latest member
Greg L.

Latest Threads

Top