Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > newbie C Syntax Question

newbie C Syntax Question
Thread Tools
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Dec 16, 2001, 04:16 AM
 
Hi here,

I really don't understand the syntax of the following line of code (from Apple's OutlineView sample code.)

I tried to search on Google for the answer - but the question mark makes this difficult.

I would also look this up in my copy of "A Book on C" but It's in London and I'm not!

Apologies for asking such an obvious question!

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (int)outlineView NSOutlineView *)outlineView numberOfChildrenOfItem id)item {
return (item == nil) ? <font color = blue>1</font> : [item numberOfChildren];
} </font>[/code]

It's the ?1: part that I don't understand. What does it mean?
     
Junior Member
Join Date: Mar 2001
Status: Offline
Reply With Quote
Dec 16, 2001, 04:54 AM
 
The ? : operator is the one and only trinary operator in C. Its syntax is:

exp1 ? exp2 : exp3

First exp1 is evaluated. If it evaluates to true, then exp2 is evaluated, and the result of the entire expression is the value of exp2. Otherwise, exp3 is evaluated, and the result of the entire expression is exp3. Note that exp2 and exp3 must have the same type, and the type of the entire expression is that type.

So for the expression

(item == nil) ? 1 : [item numberOfChildren]

If item==nil evaluates to true (i.e. item is nil), then the expression evaluates to 1, and 1 is returned. Otherwise, the expression evaluates to [item numberOfChildren].

Any book on C should cover this; you would be well served by finding one.

-Peter
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Dec 16, 2001, 05:02 AM
 
Thanks very much. very helpful.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:06 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2