Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 4 May 2008 13:34:03 +0200
From:      Roland Smith <rsmith@xs4all.nl>
To:        Unga <unga888@yahoo.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Variable arg function question
Message-ID:  <20080504113403.GA51963@slackbox.xs4all.nl>
In-Reply-To: <404840.88790.qm@web57007.mail.re3.yahoo.com>
References:  <20080504075517.GA46776@slackbox.xs4all.nl> <404840.88790.qm@web57007.mail.re3.yahoo.com>

index | next in thread | previous in thread | raw e-mail

[-- Attachment #1 --]
On Sun, May 04, 2008 at 04:01:39AM -0700, Unga wrote:
> > > 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.

Not really within the bounds of the C language.

> > > 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?

You could write a custom preporcessor that translates f calls into calls
for _f.

Roland
-- 
R.F.Smith                                   http://www.xs4all.nl/~rsmith/
[plain text _non-HTML_ PGP/GnuPG encrypted/signed email much appreciated]
pgp: 1A2B 477F 9970 BA3C 2914  B7CE 1277 EFB0 C321 A725 (KeyID: C321A725)

[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (FreeBSD)

iEYEARECAAYFAkgdnysACgkQEnfvsMMhpyVSYQCeLpjQ1+vqsghWUfPsywsc08QS
UFkAnAgRp6QpYQKCYxzSzQDDJKcTDwx5
=so2d
-----END PGP SIGNATURE-----
help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080504113403.GA51963>