Problem with if-condition with assignment on either side of logical comparison 
November 16th, 2008, 05:15 PM
| | | |
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 | 
November 16th, 2008, 06:35 PM
| | | | re: Problem with if-condition with assignment on either side of logical comparison
"Andreas Eibach" <aeibach@mail.comwrites:
<snip> Quote:
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.
<snip> Quote: |
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?
--
Ben. | 
November 16th, 2008, 06:55 PM
| | | | re: Problem with if-condition with assignment on either side of logical comparison
On Nov 16, 7:16 pm, "Andreas Eibach" <aeib...@mail.comwrote: Quote:
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. | 
November 16th, 2008, 09:15 PM
| | | | re: Problem with if-condition with assignment on either side of logical comparison vippstar@gmail.com writes: Quote:
On Nov 16, 7:16 pm, "Andreas Eibach" <aeib...@mail.comwrote: Quote:
>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.
| 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
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" | 
November 16th, 2008, 10:25 PM
| | | | re: Problem with if-condition with assignment on either side of logical comparison
Andreas Eibach wrote: Quote:
>
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)))
| .... Quote:
>
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?
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section. | 
November 16th, 2008, 10:25 PM
| | | | re: Problem with if-condition with assignment on either side of logical comparison
Andreas Eibach wrote: Quote:
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. Quote:
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.
--
Best regards,
Andrey Tarasevich | 
November 16th, 2008, 10:35 PM
| | | | re: Problem with if-condition with assignment on either side of logical comparison
On 16 Nov 2008 at 22:17, CBFalconer wrote: Quote: |
I have no idea what uchar and ulong represent.
| Hanlon's Razor makes you the biggest idiot on the planet. | 
November 16th, 2008, 11:35 PM
| | | | re: Problem with if-condition with assignment on either side of logical comparison
CBFalconer wrote: Quote:
Andreas Eibach wrote: Quote:
uchar byte1, byte2;
ulong a1, a2, b1, b2;
uchar getOffset(ulong, ulong);
uchar getOtherOffset(ulong, ulong);
byte1 = getOffset(a1, a2);
byte2 = getOtherOffset(b1, b2);
| | [...] Quote:
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). </sarcasm> Quote:
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. | 
November 16th, 2008, 11:55 PM
| | | | re: Problem with if-condition with assignment on either side of logical comparison
blargg wrote: Quote:
CBFalconer wrote: Quote:
>Andreas Eibach wrote:
>> Quote:
>> uchar byte1, byte2;
>> ulong a1, a2, b1, b2;
>> uchar getOffset(ulong, ulong);
>> uchar getOtherOffset(ulong, ulong);
>>>
>> byte1 = getOffset(a1, a2);
>> byte2 = getOtherOffset(b1, b2);
| | [...] Quote:
>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). </sarcasm>
> Quote:
>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.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|