www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - about Warning 2: possible unintended assignment

↑ ↓ ← radopas hotmail.com writes:
int *ptr=...;
while(*ptr++);
..
At the while loop dmc gives "Warning 2: possible unintended assignment".
what code is generated in this case?
I mean will it work as it is expected or the optimizer will remove it, or
something else.

best regards
rado
Oct 08 2002
bw <bw_member pathlink.com> writes:
In article <anvhit$2um5$1 digitaldaemon.com>, radopas hotmail.com says...
int *ptr=...;
while(*ptr++);
..
At the while loop dmc gives "Warning 2: possible unintended assignment".

dang i couldn't get that error either... did get Warning 7: possible extraneous ';' wonder what's up? i have sc.exe and dmc.exe v8.29n dated 7/24/02 22:09 while(*ptr++); hmmm...?
Oct 08 2002
↑ ↓ → Heinz Saathoff <hsaat bre.ipnet.de> writes:
bw schrieb...
while(*ptr++);
..
At the while loop dmc gives "Warning 2: possible unintended assignment".


Unintended assignment warnings will be given with code like this int tmp; int *ptr; // .... while(tmp=*ptr++) { some_code } ^^^^^^ Should this really be a assignment or was a compare intended. This was a common error I made when I moved from Pascal to C.
 dang i couldn't get that error either... 
 did get Warning 7: possible extraneous ';'
 
 wonder what's up?
 i have sc.exe and dmc.exe v8.29n dated 7/24/02 22:09
 
 while(*ptr++);  hmmm...?

This warning is given because the ; could have been set unintentionally, as in while(*ptr++); //<< unwanted ';' here? Function(ptr-1); The programmer want's to call Function in the while loop. But instead the Function is only called once when the while loop is finished. In both cases the code will work as written (intended). - Heinz
Oct 09 2002
→ "Walter" <walter digitalmars.com> writes:
<radopas hotmail.com> wrote in message
news:anvhit$2um5$1 digitaldaemon.com...
 int *ptr=...;
 while(*ptr++);
 ..
 At the while loop dmc gives "Warning 2: possible unintended assignment".
 what code is generated in this case?
 I mean will it work as it is expected or the optimizer will remove it, or
 something else.

It'll work fine, it's just that many programmers adopt a style avoiding assignment statements that are used as booleans. You can avoid it by writing as: while (*ptr++ != 0) ;
Oct 08 2002