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 > Variable length argument lists

Variable length argument lists
Thread Tools
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 2, 2003, 02:26 AM
 
I'm working on a tree class, and I need to use variable length argument lists; however, my understanding of them is a little fuzzy. Will this snippet yield the expected result?

Code:
- (id)initWithObject:(id)object children:(id<ITreeNode>)firstChild, ... { va_list ap; id *temp; if(self = [super init]) { ASSIGN(_object, object); if(firstChild) { _children = [[NSMutableArray alloc] init]; va_start(ap, firstChild); for(temp = firstChild; *temp != nil; temp++) { [_children addObject:*temp]; } va_end(ap); } } return self; }
     
int69h  (op)
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 2, 2003, 02:42 AM
 
Do I even need to use the va_* stuff? The call stack should come in something like otherStuff, firstAddress, secondAddress, ..., nil shouldn't it? I think I'm just confusing myself more.
     
int69h  (op)
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 2, 2003, 02:54 AM
 
Decided that I really do need the va_* stuff. What if the stack grows in the opposite direction?
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Mar 2, 2003, 05:57 AM
 
Another solution would of course be:

Code:
- (id)initWithObject:(id)object children:(NSArray*)children { ... }
     
Forum Regular
Join Date: Aug 2000
Location: UK
Status: Offline
Reply With Quote
Mar 2, 2003, 08:49 AM
 
Check out the GNUStep implementation of -[NSArray initWithObjects]:

link
     
int69h  (op)
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 2, 2003, 02:21 PM
 
Thanks for the link. I had already consulted that file actually. My original attempt was a synthesis of _initWithObjects: rest: and K&R's minprintf example.

Figured out my problem. I'll post the code in case anyone else ever has questions about this.

Code:
- (id)initWithObject:(id)object children:(id<ITreeNode>)firstChild, ... { va_list ap; id child; NSMutableArray *children; if(self = [super init]) { if(firstChild) { children = [[NSMutableArray alloc] init]; AUTORELEASE(children); va_start(ap, firstChild); for(child = firstChild; child != nil; child = va_arg(ap, id)) { [children addObject:child]; } va_end(ap); } self = [self initWithObject:object childArray:children]; } return self; }
     
   
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 08:50 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