You're right; the basic format is an (if ? then : else) statement, and the whole thing evaluates to a single expression of the same type as "then" or "else".
Quote:
int base = skull_port_base ? skull_port_base
: SKULL_PORT_FLOOR;
|
Let's break this down. The “int base” means to define a new integer variable called “base”, and this line of code will initialise it (set its starting value).
The initial value will be the result of the expression “skull_port_base ? skull_port_base : SKULL_PORT_FLOOR;”
“skull_port_base” is a variable that is an integer (or can be cast to an integer, such an unsigned integer or a char, or a C++ bool).
When testing to see if some expression is true or not, C (and C++) first casts it to an integer, and then checks to see if it equals zero. To veryfy this, note the following program prints 0:
Code:
#include <stdio.h>
void main() {
printf("%d\n", 1 == 2);
}
If an expression evaluates as zero, it is considered to be false. Anything else is true.
So if “skull_port_base” is not zero, the conditional expression will evaluate as the value of “skull_port_base”. If it is zero, it will evaluate to “SKULL_PORT_FLOOR” instead.
Hope that helps,
— Robert J. Lee