From owner-freebsd-questions@FreeBSD.ORG Sun May 4 11:01:40 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 83673106566C for ; Sun, 4 May 2008 11:01:40 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: from web57007.mail.re3.yahoo.com (web57007.mail.re3.yahoo.com [66.196.97.111]) by mx1.freebsd.org (Postfix) with SMTP id 451D08FC1D for ; Sun, 4 May 2008 11:01:40 +0000 (UTC) (envelope-from unga888@yahoo.com) Received: (qmail 92026 invoked by uid 60001); 4 May 2008 11:01:39 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=dzV+GbFlqDddiltOCUcGjFS33ZBzAANAZCgw4whCdr8j/iyA4btH+ysL2ppX3+bF0c96HIwptGZaPr8bwSLIDrLNuUs1fbCevFmVdSLKcBY7UdJH7BEUJjV1YGlsTcbyH6HNrjCgE3GO3trFS2p1Iak1SrY303JwvxluVJWrqLc=; X-YMail-OSG: HogAsCkVM1kjrts51rGcK0CpXcOXJsX_iZZuFANii5jXnfdYt4IqPvIniYtwhvrcfw-- Received: from [165.21.155.13] by web57007.mail.re3.yahoo.com via HTTP; Sun, 04 May 2008 04:01:39 PDT Date: Sun, 4 May 2008 04:01:39 -0700 (PDT) From: Unga To: Roland Smith In-Reply-To: <20080504075517.GA46776@slackbox.xs4all.nl> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <404840.88790.qm@web57007.mail.re3.yahoo.com> Cc: freebsd-questions@freebsd.org Subject: Re: Variable arg function question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 04 May 2008 11:01:40 -0000 --- Roland Smith 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