455,433 Members | 1,702 Online Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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 Nov 16 '08 #1
8 Replies

 P: n/a "Andreas Eibach" 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? -- Ben. Nov 16 '08 #2

 P: n/a On Nov 16, 7:16 pm, "Andreas Eibach"

 P: n/a vi******@gmail.com writes: On Nov 16, 7:16 pm, "Andreas Eibach" 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) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 16 '08 #4

 P: n/a Andreas Eibach wrote: > 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? -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: Try the download section. Nov 16 '08 #5

 P: n/a Andreas Eibach wrote: 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. -- Best regards, Andrey Tarasevich Nov 16 '08 #6

 P: n/a On 16 Nov 2008 at 22:17, CBFalconer wrote: I have no idea what uchar and ulong represent. Hanlon's Razor makes you the biggest idiot on the planet. Nov 16 '08 #7

 P: n/a CBFalconer wrote: Andreas Eibach wrote: 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). 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. Nov 16 '08 #8

 P: n/a blargg wrote: CBFalconer wrote: >Andreas Eibach wrote: >> 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 aresupposed to mean "unsigned char" and "unsigned long", just writethe real meanings. Obviously it would have taken too much time to write "unsigned char" instead of uchar. His time is very valuable (unlike ours). >Assuming so, how can the "getOffset" call everwork, since you are passing char addresses to a routine that wantsunsigned 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]: Try the download section. Nov 16 '08 #9

### This discussion thread is closed

Replies have been disabled for this discussion. 