syntax question

Hi!
Sorry for the maybe silly question but I'm a bloody beginner in C/C++.
What does the following line mean?
I know the "if(..." statement but not the term after "if(".

 
  if( (a = *b++) != 0 ) { ...

Last edited on
It's as if you had written
1
2
3
a = *b;  // Assign to a, what b points to
b++;     // Increment b to point to the next thing
if ( a != 0 ) {  // do something if a is not equal to 0 
Last edited on
Look at: http://www.cplusplus.com/doc/tutorial/operators/
At the end of it is an operator precedence table.

You have an if-statement. It contains an expression:
(a = *b++) != 0

The parentheses make it clear that a = *b++ has to be evaluated before the X != 0.

The condition is true, if the value of a = *b++ is not zero.

The precedence table shows that the assignment operator = is after the other.

The value of a = Y is the value of a after the assignment and thus equal to value o Y.

*b++ has dereference (*) and postfix increment (++). These too do have clear precedence.
1
2
3
*b++
// equals
*( b++ )


Lets evaluate:
1. Make temporary copy (Z) of b
2. Increment b
3. Dereference Z to get value W
4. Assign W to a
5. Check if a is (not) 0
Great!
Thank you!
Topic archived. No new replies allowed.