www.digitalmars.com         C & C++   DMDScript  

c++ - about Warning 2: possible unintended assignment

reply 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
next sibling parent reply 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
parent 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
prev sibling parent "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