Problem with if-condition with assignment on either side of logical comparison

A

Andreas Eibach

Hi,

supposed I have this alone:
(mentally add typedefs :))

uchar byte1, byte2;
ulong a1, a2, b1, b2;
uchar getOffset (ulong, ulong);
uchar getOtherOffset (ulong, ulong);

byte1 = getOffset (a1, a2);
byte2 = getOtherOffset (b1, b2);

_Instead_of the above, to shorten it all, it did NOT work here to say

if ( (byte1 = getOffset (a1,a2)) != (byte2 = getOtherOffset(b1,b2)))
{
....
}

It was supposed to have both byte1 and byte2 calculated separately, then
compared.
No errors whatsoever when compiling; however, this always gave a wrong
(resp. random) result for byte1 when debugging, why?

However, calculating the left result isolatedly, this *does* work:

byte1 = getOffset (a1,a2);
if ( (byte1 != (byte2 = getOtherOffset(b1,b2)))
{
....
}

Is this a known "issue"? (note the quotes)

-Andreas
 
B

Ben Bacarisse

Andreas Eibach said:
uchar byte1, byte2;
ulong a1, a2, b1, b2;
uchar getOffset (ulong, ulong);
uchar getOtherOffset (ulong, ulong);

byte1 = getOffset (a1, a2);
byte2 = getOtherOffset (b1, b2);

_Instead_of the above, to shorten it all, it did NOT work here to say

if ( (byte1 = getOffset (a1,a2)) != (byte2 = getOtherOffset(b1,b2)))
{
...
}

It would be best to post a complete compilable example. Just making
such an example often reveals the problem but even if it does not it
gives us something try out.

Since you are suggesting a compiler fault, the name and version of it
would help.

Is this a known "issue"? (note the quotes)

Not to me, sorry. BTW, I noted the quotes but I have no idea what
they signify. What is an "issue" as opposed to an issue?
 
V

vippstar

Hi,

supposed I have this alone:
(mentally add typedefs :))

uchar byte1, byte2;
ulong a1, a2, b1, b2;
uchar getOffset (ulong, ulong);
uchar getOtherOffset (ulong, ulong);

When you write a usenet post, expect it to be treated the same way you
treated it; A serious post will get serious replies (minus trolls), a
half-arsed post will receive half-arsed replies.
 
K

Keith Thompson

When you write a usenet post, expect it to be treated the same way you
treated it; A serious post will get serious replies (minus trolls), a
half-arsed post will receive half-arsed replies.

Composing a good question, like programming in C, is a learned skill.
Advising the OP how he can ask better questions (as several others in
this thread have done) is likely to be more constructive than
insulting him.

One good resource:
http://www.catb.org/~esr/faqs/smart-questions.html
 
C

CBFalconer

Andreas said:
uchar byte1, byte2;
ulong a1, a2, b1, b2;
uchar getOffset(ulong, ulong);
uchar getOtherOffset(ulong, ulong);

byte1 = getOffset(a1, a2);
byte2 = getOtherOffset(b1, b2);

_Instead_of the above, to shorten it all, it did NOT work here

if ((byte1 = getOffset(a1,a2)) != (byte2 = getOtherOffset(b1,b2))) ....

It was supposed to have both byte1 and byte2 calculated separately,
then compared. No errors whatsoever when compiling; however, this
always gave a wrong (resp. random) result for byte1 when debugging,
why?

I have no idea what uchar and ulong represent. If they are
supposed to mean "unsigned char" and "unsigned long", just write
the real meanings. Assuming so, how can the "getOffset" call ever
work, since you are passing char addresses to a routine that wants
unsigned long addresses?
 
A

Andrey Tarasevich

Andreas said:
Hi,

supposed I have this alone:
(mentally add typedefs :))

uchar byte1, byte2;
ulong a1, a2, b1, b2;
uchar getOffset (ulong, ulong);
uchar getOtherOffset (ulong, ulong);

byte1 = getOffset (a1, a2);
byte2 = getOtherOffset (b1, b2);

_Instead_of the above, to shorten it all, it did NOT work here to say

if ( (byte1 = getOffset (a1,a2)) != (byte2 = getOtherOffset(b1,b2)))
{
...
}
It was supposed to have both byte1 and byte2 calculated separately, then
compared.
No errors whatsoever when compiling; however, this always gave a wrong
(resp. random) result for byte1 when debugging, why?

This should give you the result identical to the original code (i.e.
'byte1' and 'byte2' calculated in advance, separately, and then
compared), unless 'getOffset' has side-effects that affect the result of
'getOtherOffset' or vice versa. The language specification does not
specify whether 'getOffset (a1,a2)' or 'getOtherOffset(b1,b2)' should be
called first in the "shortened" version.
However, calculating the left result isolatedly, this *does* work:

byte1 = getOffset (a1,a2);
if ( (byte1 != (byte2 = getOtherOffset(b1,b2)))
{
...
}

.... which is a strong indication of the existence of the aforementioned
side-effects.
 
C

CBFalconer

blargg said:
CBFalconer said:
Andreas said:
uchar byte1, byte2;
ulong a1, a2, b1, b2;
uchar getOffset(ulong, ulong);
uchar getOtherOffset(ulong, ulong);

byte1 = getOffset(a1, a2);
byte2 = getOtherOffset(b1, b2);
[...]
I have no idea what uchar and ulong represent. If they are
supposed to mean "unsigned char" and "unsigned long", just write
the real meanings.

Obviously it would have taken too much time to write "unsigned char"
instead of uchar. His time is very valuable (unlike ours). said:
Assuming so, how can the "getOffset" call ever
work, since you are passing char addresses to a routine that wants
unsigned long addresses?

Huh? He's passing type ulong to functions accepting ulong, and
assigning the uchar result to uchar objects. No addresses involved.

True. I saw the 'getOffset' and assumed he was passing addresses.
The code just doesn't make any sense.
 

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
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top