1
if ((vnd = (struct diam_vnd_t *)g_hash_table_lookup(vendors,vend))) {...}

Can you tell me why it is an assignment but not a boolean expression in the brackets ? And in what situation this assignment can be considered "true" or "false" ?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
SWIIWII
  • 387
  • 5
  • 15

4 Answers4

4

Quoting C11, chapter §6.5.16, Assignment operators (emphasis mine)

An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment,111) but is not an lvalue.

So, first the assignment will happen, and then, the value that has been assigned will be used as the conditional statement in if.

So, in case of

if (p = 0 )

will evaluate to FALSE and

if (p = 5)

will be TRUE.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

C considers anything non-zero to be true, and anything 0 to be false.

This assignment's value is equal to the value it assigns to vnd, in this case, a struct diam_vnd_t *. The if statement checks whether or not vnd is NULL after the assignment.

This would be equivalent to:

vnd = (struct diam_vnd_t *)g_hash_table_lookup(vendors,vend);
if (vnd) {...}
Alexander
  • 59,041
  • 12
  • 98
  • 151
  • C very well has a boolean type. And in a condition, any expression is converted to a boolean result (of type `int`, though). – too honest for this site May 11 '16 at 15:19
  • The `bool` type is just an `unsigned int`. Fundamentally, the language deals with "zero" and "non-zero" as booleans, rather than `true`/`false` atoms. – Alexander May 11 '16 at 15:37
  • No, `_Bool` is a distinct unsigned integer type with the values `0` and `1` `bool` is a macro in `stdbool.h` which maps to `_Bool`. It is **not** `unsigned int`. Note: This is standard C, not some homebrew `typedef`! About the "true"/"false" interpretation: Try `_Bool b = 4;` or `printf("%d", (bool)5);`. Note that boolean logic does not define how "true"/"false" have to be represented. Often they are by `0`/`1`, because that simplifies mathematical operations. See Wikipedia. – too honest for this site May 11 '16 at 15:48
  • It would be better to read the C standard directly (specifically 6.2.5p2 and p6 e.g.) instead of a second/third source. Maybne you will find some other new features since the last 17 years. You might also find that `NULL` is a macro with a _null pointer constant_, not a specific value. A pointer variable can only be a _null pointer_. And that is not required to have a binary representation of all-zeros. Still, such a null pointer has to evaluate "false". – too honest for this site May 11 '16 at 17:28
0

The logical operator for "is equal to" is ==

When you're saying vnd = (struct... you are assigning everything after = to the variable vnd. If you want a true or false you need to use ==

CaldwellYSR
  • 3,056
  • 5
  • 33
  • 50
0

Assignment is always done with one equal sign. =

int i;
i = 0; //assignment

This assigns 0 to an integer called i.

The same thing happens with your if statement. Whether or not it is in an if statement is irrelevant.

(vnd = (struct diam_vnd_t *)g_hash_table_lookup(vendors,vend))

To do a boolean expression, you need to use ==.

(vnd == (struct diam_vnd_t *)g_hash_table_lookup(vendors,vend))

This will return true or false based on the comparison of the 2 items

element11
  • 4,218
  • 2
  • 16
  • 22