Using the result of an assignment as a condition without parentheses

22,128

Solution 1

This is probably not an error as you said, it's a warning.

The compiler is warning you that you should surround assignments within parentheses when it is within a conditional to avoid the ol' assignment-when-you-mean-comparison mistake.

To get past this rather pedantic compiler warning, you can simply surround the assignment within another pair of parentheses:

while((title = va_arg(args,NSString*))) {
//...
}

Solution 2

It should be a warning and not an error. It's trying to warn in case you're using = but you meant ==.

In this case, you're not meaning to use == because you're calling va_arg() multiple times to iterate through otherButtonTitles and assigning it to the temp var title, so just add another set of parentheses. The warning will go away.

while((title = va_arg(args,NSString*))) {

Solution 3

compiler thinks that you're doing assignment by mistake, hence suggesting this error. You should wrap your statement into ONE MORE PAIR of parenthesis like this:

while((title = va_arg(args,NSString*)))

Then compiler first will evaluate the assignment, then the result of that assignment will be passed into the while condition

Solution 4

It does seem a little picky, but have you tried:

while((title = va_arg(args,NSString*)) != NULL)

?

Share:
22,128

Related videos on Youtube

Duck
Author by

Duck

Updated on April 27, 2020

Comments

  • Duck
    Duck over 2 years

    I have this inside a custom UIActionSheet class

    if (otherButtonTitles != nil) {
        [self addButtonWithTitle:otherButtonTitles];
        va_list args;
        va_start(args, otherButtonTitles);
        NSString * title = nil;
        while(title = va_arg(args,NSString*)) { // error here
            [self addButtonWithTitle:title];
        }
        va_end(args);
    }
    

    I have this error

    ! using the result of an assignment as a condition without parentheses

    pointing to this line

    while(title = va_arg(args,NSString*)) {
    

    why is that?

    thanks.

  • Duck
    Duck about 12 years
    thanks!!! That solved the problem. The most strange part is that only LLVM is picky about it. GCC compiles it without warning. I am starting to use LLVM to compile, instead of GCC and I am receiving some picking errors here and there...
  • BoltClock
    BoltClock about 12 years
    @Digital Robot: I remember reading somewhere that specifically says LLVM checks for this. I think it's good to be picky about these things sometimes :)
  • Brad Larson
    Brad Larson about 12 years
    @Digital Robot - GCC will do this with the -Wparentheses option enabled (the Missing Braces and Parentheses warning in Xcode). I believe LLVM does this by default now.
  • SinisterMJ
    SinisterMJ about 12 years
    LLVM has a ton more warnings for things that are usually errors - liek this example, most people with "=" in an if statement have messed something up. Also, it checks the types of what you are passing in to a string format to make sure they match - really helpful. Note that some people have seen wierd issues when using LLVM though, so just be aware you might run into problems and have to switch back to LLVM-GCC at some point.
  • Duck
    Duck about 12 years
    Yes, I have triple checked and everything is running fine with the binary compiled with LLVM. No problems so far.
  • Max MacLeod
    Max MacLeod over 11 years
    strangely the issue appears as a warning in the editor but not in the build results
  • dylam
    dylam over 6 years
    Why does surrounding the assignment in parentheses make a difference?
  • Greg Schmit
    Greg Schmit over 4 years
    @dylam it is a clear signal that you want to use an assignment in the conditional, and not an accidental forgot-an-extra-equals-sign mistake. It's not that context cannot help you determine this also, but the extra parenthesis serve as a flag that this is intended. It's not an error; it's a warning.