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.
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.
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.
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.
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);
}