views:

79

answers:

3

I use the following to work with arguments in my programs, but it seems to just hand me a warning (just a warning): "warning: suggest parentheses around assignment used as truth value"

The beginning of the code is as follows:

   while((++argv)[0] && argv[0][0]=='-'){
      while(c =* ++argv[0])

The while(c =* ++argv[0]) part being where the warning persists. The code works fine, but what does this warning mean opposed to what is used?

I think the code is c = *++argv[0], using the pointer. So why does the single = work and what is really recommended to be used?

+4  A: 

The compiler is unsure whether you intended

while(c == whatever)

instead. It's a common error to misuse assignment instead of comparison inside the conditional expression. Sometimes you do want assignment, sometimes not and the compiler gives a warning. To make the compiler see that you really want assignment and not emit the warning use:

while((c =* ++argv[0]) != 0)
sharptooth
+1  A: 

you need to double the parenthesis to explicitly state that you really mean to test the value of c:

while( (c =* ++argv[0]) )
Nikko
+1  A: 

The compiler warns because you might accidently have written a statement instead of an expression. By embedding the statement in parentheses you tell the compiler your intention and stop him warning.

ur