Date: Sun, 4 May 2008 04:01:39 -0700 (PDT) From: Unga <unga888@yahoo.com> To: Roland Smith <rsmith@xs4all.nl> Cc: freebsd-questions@freebsd.org Subject: Re: Variable arg function question Message-ID: <404840.88790.qm@web57007.mail.re3.yahoo.com> In-Reply-To: <20080504075517.GA46776@slackbox.xs4all.nl>
next in thread | previous in thread | raw e-mail | index | archive | help
--- Roland Smith <rsmith@xs4all.nl> wrote: > On Sun, May 04, 2008 at 12:40:43AM -0700, Unga > wrote: > > Hi all > > > > I need to implement a variable argument function > in C. > > The number of args are not known but the type is > > known, all are strings. > > > > Unfortunately va_arg() [stdarg(3)] does not return > > NULL or any other suitable value after processing > the > > arg list, it just simply crashes once the arg list > is > > exhausted. > > It is _your_ task to properly close the argument > list. E.g. by supplying > a NULL pointer as the last argument. > Infact, I have implemented it in this way. I was wondering if there is a better way. The issue I see is, if someone forget to close the arg list with NULL pointer, it crashes. > > How do you guys implement variable arg function > such > > as f(str1, str2, str3, ..., strN)? > > you could use the same format as main: int foo(int > num, char **args) > This is interesting. Who set the num? The compiler or the user. If it is the user, its no better than above NULL pointer method. If this is possible, my problem is solved: f(str1, str2, str3, ..., strN) is at compile time expands to _f(int num, str1, str2, str3, ..., strN). The num is set automatically by the compiler by counting the args. Is this possible? Unga ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?404840.88790.qm>