Path: utzoo!attcan!uunet!cs.utexas.edu!wuarchive!julius.cs.uiuc.edu!apple!portal!sv!moose From: moose@svc.portal.com Newsgroups: comp.sys.next Subject: Re: methods with arbitrary # of args? How? Keywords: varargs Message-ID: <1990Oct16.164405.9946@svc.portal.com> Date: 16 Oct 90 16:44:05 GMT References: <3860@network.ucsd.edu> Followup-To: comp.lang.objective-c Organization: Software Venture Lines: 31 In article <3860@network.ucsd.edu> pbiron@weber.ucsd.edu (Paul Biron) writes: >Is it possible to define an obj-c method which takes an >arbitrary number of arguments (ala normal C functions >which use varargs.h)? > >If so, how? If not, does anybody have an "elegent" way >to simulate this kind of behavior? Everything is possible and elegant is relative. Try this: - myMethod:(int) argc :(char **)argv; When you call it, malloc out an array of pointers for argv, then malloc out each sub pointer, then, voila, you have an arbitrary number of arguments. If your argv is small, and/or your entire data is small, you might want to use alloca instead of malloc. alloca does not need corresponding calls to free. Similarly - myMethod:(char *)pattern :(void *)data; lets you do something along the line of printf. Matter of fact, using sprintf, you can fill the data with your arguments relatively easilty. -- Michael Rutman | moose@svc.portal.com Cubist | makes me a NeXT programmer Software Ventures | That's in Berkeley smile, you're on standard disclaimer |