Pergunta

Output of the following program is : 3 1 3

int main()
{
    int a = 0, b = 1, c = 3;
    *((a) ? &b : &a) = a ? b : c; // Couldn't understand expression
    printf("%d %d %d \n", a, b, c);
    return 0;
}

How ternary operator is working here for this output?

Foi útil?

Solução

 *( (a) ? &b : &a) =     a ? b : c ;
     ^                   ^
     = 0                 = 0
     ==false             ==false

so the above expression:

 *( &a) =  c ;

that is:

     a =  c ;

because c = 3;, so it give:

     a =  3 ;   

b is 1 and c is 3 in your declaration(remains unchanged).

Outras dicas

My C is a little weak, so if I'm off-base here, someone let me know.

Assuming you're talking about the expression *((a) ? &b : &a) (and that you know how the ternary operator works generally), here's how it breaks down:

The condition (a) is evaluated: when using an int in a boolean, 0 evaluates to false, while positive (or non-zero?) values evaluate to true — in this case, a == 0 is false.

The ternary then returns the value &b if true and &a if false (in this case it's false, so it returns &a).

The value thus returned is a reference to a variable; the asterisk before the surrounding parentheses dereferences this reference, so the expression *((a) ? &b : &a) evaluates to the variable — but not the value of — a (if a where not 0, the expression would evaluate to b).

That line then assigns to this variable (in this case a) the value of the second ternary expression, which I assume you can figure out.

The final effect is that it assigns the value of c to the variable a, which explains the output.

Note that the ternary conditional operator has higher precedence than the assignment operator. The ternary conditional has the form:

condition ? true-result : false-result

If condition is true, the result is true-result, otherwise the result is false-result. The operator is short-circuiting in the sense that only one of the results is ever evaluated.

This operator can be used for conditional assignment. For example:

int days_of_year = is_leap_year ? 366 : 365;

The result of the ternary conditional is an r-value. This means that the result is a value that cannot be the target of an assignment. However, there is a trick that can be used to use the ternary conditional with pointers and dereference so as to get the behavior of conditionally assigning to one variable or another.

int x;
int y;
*(need_x ? &x : &y) = 0;

While the result of a ternary condition is an r-value, the result of a dereference is an l-value. This means the result of the dereference can be used as the target of an assignment.

Your code uses both of these ideas in combination.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top