3

I have a condition and three variables.

// Assume these variables are all initialized.
int *a;
int *b;
const int c;
const bool condition;

if (condition)
    *a = c;
else
    *b = c;

I want theif block to be on one line through a ternary conditional operator.

(condition ? *a : *b) = c;

I don't see why that's not allowed as I'm not breaking the rule of ternary conditional operators. Both *a and *b return the same type.

jww
  • 97,681
  • 90
  • 411
  • 885
Hatefiend
  • 3,416
  • 6
  • 33
  • 74
  • 1
    **−1** The code is **not real code**, and the alleged fact is **not true**, and if the code is corrected it still uses C++ specific features while the question is tagged with **multiple languages**, both C++ and C. – Cheers and hth. - Alf Feb 26 '17 at 01:22
  • because this ternary conditional operator syntax is the same in both C and C++ – Hatefiend Feb 26 '17 at 02:18

3 Answers3

10

This is not allowed in C because *a is an int value after a pointer dereference and a conditional; is not an lvalue, i.e. not assignable. However, this is allowed:

*(condition ? a : b) = c;

Now that the conditional produces a pointer, and the dereference happens outside of it, the overall expression is an assignable lvalue.

You can expand this to two conditions and three pointers:

*(condition1 ? a : (condition2 ? b : c)) = d;
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Let's say `c` was an `int*`. How would I assign `a` or `b` to `c` depending on the `condition`? No matter how I type it my editor doesn't allow it. – Hatefiend Feb 25 '17 at 09:05
  • @Hatefiend You can't use one condition to decide among three options. You need a second condition (see edit). – Sergey Kalinichenko Feb 25 '17 at 09:08
  • No no, I only have one `condition`. [Here is my actual source code where I am currently getting the problem](http://i.imgur.com/ZXg9yQT.png). It has the same error you talked about `not an !value`. In this screenshot, both `a` `b` and `c` are all of type `struct Node*`. I don't get what's wrong. None of the variables are `const`. – Hatefiend Feb 25 '17 at 09:11
  • Wait... [am I not allowed to use this syntax in C?](http://stackoverflow.com/a/40389972/4718288) – Hatefiend Feb 25 '17 at 09:14
  • @Hatefiend Then you need to take address and use `*`, like this: `*(condition ? &a : &b) = c` – Sergey Kalinichenko Feb 25 '17 at 09:14
  • @Hatefiend Yes, you are allowed to use it, but it's not a very good practice. An `if` statement is more readable. – Sergey Kalinichenko Feb 25 '17 at 09:15
  • Wow. `*(condition ? &a : &b) = c`. That's incredible. I thought about using address of but I was like, "no no, surely there must be a better way". Thanks for the help. – Hatefiend Feb 25 '17 at 09:26
1

In C++ there's nothing wrong with

(condition ? *a : *b) = c;

itself.

It's all OK as far as the rules of C++ are concerned.

Possible problems include that you may be using uninitialized pointers, which the compiler is then likely to complain about. But this is impossible to say for sure, because like the incorrect claim of the above not being valid, the code you've posted is ¹not real code.


¹ Please only post real code in your questions, and please don't offer wild assumptions as alleged facts, because this wastes everyone's time and can mislead people coming here via Google.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Like I said, I put the comments above saying that all the variables are initialized. Once you start initializing stuff with certain values, StackOverflow will start to find alternatives ways to answer the question based on those values and not necessarily answer the concept of the question. They may ask why you are trying to do x and not y. etc. That's why it's just easier to throw in an assumption. – Hatefiend Feb 26 '17 at 02:16
0

You can write it either like this

((condition) ? *a : *b) = c;

or like this

(condition) ? *a = c : *b = c;
Lunatoid
  • 68
  • 9