I do not understand the termination parameter of this for loop. What does it mean? Specifically, what do the ?, ->, and : 0 represent?
for( i = 0; i < (sequence ? sequence->total : 0); i++ )
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This:
(sequence ? sequence->total : 0)(it’s called a “ternary if”, since it takes three inputs) is like saying:->is a dereferencer, just like*, but it makes user data-types likestructs easy to use.sequence->totalmeanssequenceis a pointer to a one of those data types, and you are accessing thetotalproperty of what it is pointing to. It’s exactly the same as:So the loop:
exits when
sequenceevaluates tofalse, since0 == false.The ternary if construction is used to make sure they aren’t dereferencing (
->) a null pointer, because if they just putsequence->totalas the condition, they would be dereferencing it every time. Unhappy! =(