Thread overview
about Warning 2: possible unintended assignment
Oct 08, 2002
radopas
Oct 09, 2002
bw
Oct 09, 2002
Heinz Saathoff
Oct 09, 2002
Walter
October 08, 2002
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


October 09, 2002
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...?


October 09, 2002
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


October 09, 2002
<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)
        ;