• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# What does this line of C do? Never seen anything like it before...

## Recommended Posts

Promit    13246
I was going through different samples of stuff I had on D3D, and i came across this line: d3dpp.bWindowed = bWindowed ? d3ddm.Format : FullScreenFormat Where bWindowed is a BOOL and FullScreenFormat is D3DFORMAT. I have no clue what that is supposed to do. There''s nothing in MSDN that I could find on ? or : operators in that syntax either. ----------------------------- The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

##### Share on other sites
It''s like an if-statment.

It translates into:

if (bWindowed)
d3dpp.bWindowed = d3ddm.Format;
else
d3dpp.bWindowed = FullScreenFormat;

##### Share on other sites
Guest Anonymous Poster
quote:
Original post by Promit
I was going through different samples of stuff I had on D3D, and i came across this line:
d3dpp.bWindowed = bWindowed ? d3ddm.Format : FullScreenFormat
Where bWindowed is a BOOL and FullScreenFormat is D3DFORMAT. I have no clue what that is supposed to do. There''s nothing in MSDN that I could find on ? or : operators in that syntax either.

Syntax

logical-OR-expr ? expr : conditional-expr

Remarks

The conditional operator ?: is a ternary operator.

In the expression E1 ? E2 : E3, E1 evaluates first. If its value is true, then E2 evaluates and E3 is ignored. If E1 evaluates to false, then E3 evaluates and E2 is ignored.

The result of E1 ? E2 : E3 will be the value of either E2 or E3 depending upon which evaluates.

E1 must be a scalar expression. E2 and E3 must obey one of the following rules:

1. Both of arithmetic type. E2 and E3 are subject to the usual arithmetic conversions, which determines the resulting type.
2. Both of compatible struct or union types. The resulting type is the structure or union type of E2 and E3.
3. Both of void type. The resulting type is void.
4. Both of type pointer to qualified or unqualified versions of compatible types. The resulting type is a pointer to a type qualified with all the type qualifiers of the types pointed to by both operands.

5. One operand is a pointer, and the other is a null pointer constant. The resulting type is a pointer to a type qualified with all the type qualifiers of the types pointed to by both operands.
6. One operand is a pointer to an object or incomplete type, and the other is a pointer to a qualified or unqualified version of void. The resulting type is that of the non-pointer-to-void operand.

##### Share on other sites
lc_gta    122
basicly its an inline conditional statement so if bWindowed is true it will evaluate to d3ddm.Format, if false it''ll be FullScreenFormat. It''s just another thing you can use in place of an if statement

##### Share on other sites
Promit    13246
SO it''s a ternary operator that deals with selection and is shorthand for single expression if...else statements. Ok, thanks.

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

##### Share on other sites
Guest Anonymous Poster
Have you tried to compile this?
If d3dpp.bWindowed is boolean and d3ddm.Format and FullScreenFormat is D3DFORMAT then I would think this is nonsensical code

##### Share on other sites
Kippesoep    892
No, read the posts above. The expression evaluates to either d3ddm.Format or FullScreenFormat, which is of the correct type.

##### Share on other sites
Guest Anonymous Poster
Kippesoep:
Look at the variable that is receiving the value from the ?: operator--

d3dpp.bWindowed = bWindowed ? d3ddm.Format : FullScreenFormat
^ it appears to be a boolean.. however, D3DFORMAT is a
32bit data type.. so unless d3dpp.bWindowed is just a poorly named variable, this wont work.

##### Share on other sites
Promit    13246
Oh sorry, I put the wrong member of the d3dpp structure, it should have been d3dpp.Format, but the important thing was the operator.

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

##### Share on other sites
Blaster    124
One of the best use I know of the ? operator is dealing with references.
  .... ( const bool bShort ){ sometype& ref = bShort ? shortthing : longthing;}

Without the ? operator the previous code would have to use a pointer, and it would clutter the code.

Just my two cents