0

What would this translate to (in a more verbose way)?

local.sin_addr.s_addr = (!ip_address) ? INADDR_ANY:inet_addr(ip_address);

Trying to understand Ternary operators and I can't quite get it.

Jack T
  • 11
  • 1
  • 3
  • 1
    That's a basic construct of the language. Please see [a beginner's text book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – R Sahu Oct 13 '16 at 04:59
  • 2
    I'm voting to close this question as off-topic because the answer to the question can be found in any beginner's text book. – R Sahu Oct 13 '16 at 05:03

3 Answers3

0

Ternarys are used when you want to assign to a variable, and you have exactly two options.

The equivalent of:

int a = true ? 1 : 2;

Is:

int a;
if (true) {
    a = 1;
} else {
    a = 2;
}

Understanding this, you should be able to translate your code.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
0

Well ternary operators is a shorthand of your if/else statement. To explain the condition above it would look like this.

(condition) ? (return true value) : (return false value)

And this could be translated into a long form statement like @Carcigenicate answer.

claudios
  • 6,588
  • 8
  • 47
  • 90
0

A ternary is similar to an if statement, but it can be used where an expression is required. So it's equivalent to:

if (!ip_address) {
    local.sin_addr.s_addr = INADDR_ANY;
} else {
    local.sin_addr.s_addr = inet_addr(ip_address);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612