🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #269

variable number of arguments in ANSI C

#269
From: Tim McDaniel
Date: Fri, 23 Jun 1989 18:45
77 lines
2684 bytes
Lars Nyman asked me to copy this over from comp.unix.questions.  His
site does not receive comp.std.c.  If you want to send e-mail, please
send it directly to his account shown on the "From:" line.

----------------------------------------------------------------------

From: NYMAN@intellicorp.com (Lars Nyman)
Newsgroups: comp.unix.questions
Subject: variable number of arguments in ANSI C
Message-ID: <20070@adm.BRL.MIL>
Date: 21 Jun 89 19:48:04 GMT

What assumptions is OK to make about variable number of arguments in ANSI C ?

Example:

#include <stdarg.h>

void foo(int i, ...)
{
  va_list args;

  va_start(args, i);         /* initialize args pointer */
  va_arg(args, char *);      /* pop the first argument */
  bar(i - 1, args);          /* let bar() see the rest, Q1 */

  va_start(args, i);         /* initialize args pointer again, Q2 */
  baz(i, args);              /* let baz() see them all, Q1 */

  va_end(args);              /* done */
}

void bar(int i, va_list args)
{
  while (i > 0) {
    printf("%s\n", va_arg(args, char *)); /* pop arguments, Q1 */
    i--;
  }
}

void baz(int i, va_list args)
{
  while (i > 0) {
    printf("%s\n", va_arg(args, char *)); /* pop arguments, Q1 */
    i--;
  }
}

main()
{
  foo(3, "Monday", "Tuesday", "Wednesday");
}



I know this will work in atleast some implementations of ANSI C, but is it
portable, legal ANSI C code ?

Q1: Is it OK to pass the va_list to another function and let that function pop
    all or some of the arguments with va_arg() ?

    I believe this is OK according to ANSI, since vprintf() etc. in the 
    standard library takes a va_list argument.  Am I correct ?

Q2: Is it OK to "restart" by calling va_start() more than once ?

Lars Nyman

nyman@intellicorp.com
--
"Let me control a planet's oxygen supply, and I don't care who makes
the laws." - GREAT CTHUHLU'S STARRY WISDOM BAND (via Roger Leroux)
 __
   \         Tim, the Bizarre and Oddly-Dressed Enchanter
    \               mcdaniel@uicsrd.csrd.uiuc.edu
    /\       mcdaniel%uicsrd@{uxc.cso.uiuc.edu,uiuc.csnet}
  _/  \_     {uunet,convex,pur-ee}!uiucuxc!uicsrd!mcdaniel
Technical Details
Message-ID: <1356@garcon.cso.uiuc.edu>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!tank!uxc.cso.uiuc.edu!garcon!uicsrd.csrd.uiuc.edu!mcdaniel

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!tank!uxc.cso.uiuc.edu!garcon!uicsrd.csrd.uiuc.edu!mcdaniel
From: mcdaniel@uicsrd.csrd.uiuc.edu (Tim McDaniel)
Newsgroups: comp.std.c
Subject: variable number of arguments in ANSI C
Message-ID: <1356@garcon.cso.uiuc.edu>
Date: 23 Jun 89 18:45:41 GMT
Sender: news@garcon.cso.uiuc.edu
Reply-To: mcdaniel@uicsrd.csrd.uiuc.edu (Tim McDaniel)
Organization: Center for Supercomputing R&D (Cedar), U. of Ill.
Lines: 77