Article View: comp.unix.programmer
Article #3511Variable length arg lists in C (gcc): stdarg vs. varargs
From: Kenny McCormack
Date: Wed, 11 Oct 2000 00:00
Date: Wed, 11 Oct 2000 00:00
39 lines
2067 bytes
2067 bytes
Notes, presented in no particular order:
1) I don't know if this is a GCC under Unix specific question (which
is my primary/only focus) or if it relates to all C compilers
and/or all platforms.
2) From what I can tell, there is a thing called varargs that is:
a) More powerful than stdarg
b) No longer documented
c) No longer supported
d) Not (as) portable
3) Here's the code that I have (I wrote it and it works fine):
/* Find the max of a list of ints, 0 marks the end of the list */
#include <stdarg.h>
int max(int n, ...)
{
va_list ap;
int maxnum = n;
va_start(ap, n);
while ((n = va_arg(ap, int)))
if (n > maxnum)
maxnum = n;
va_end(ap);
return maxnum;
}
4) Question: From what I can tell, stdarg has the following
limitations:
a) No way to tell how many args there are. I.e., you have
to have some kind of marker for the end of the list. If
you try to access more args than are passed, strange and
bad things will happen.
b) You have to have at least one fixed arg. This causes it
to fail the "acid test" - namely, "Can I write a wrapper
around printf?" This is mentioned in the man page
("man stdarg" on a Linux system).
So, have I got this all right? In particular, I am curious about the
questions raised in #4, and wondering if it is possible to use varargs
(which I can tell exist, from the header files, but I can't find docu on)
Technical Details
Message-ID:
<8s26b3$9vl$1@yin.interaccess.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,22450a31b48667a4,start X-Google-Attributes: gidfc89c,public X-Google-Thread: f65d0,22450a31b48667a4,start X-Google-Attributes: gidf65d0,public From: gazelle@yin.interaccess.com (Kenny McCormack) Subject: Variable length arg lists in C (gcc): stdarg vs. varargs Date: 2000/10/11 Message-ID: <8s26b3$9vl$1@yin.interaccess.com> X-Deja-AN: 680214784 Organization: The official candy of the new Millennium Reply-To: gazelle@interaccess.com Newsgroups: comp.unix.programmer,gnu.gcc X-Complaints-To: newsabuse@supernews.com