delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/11/19:53:53

Date: Sat, 11 Oct 1997 16:51:51 -0700 (PDT)
Message-Id: <199710112351.QAA25355@adit.ap.net>
Mime-Version: 1.0
To: Jose Luis Perandones Colino <joepc AT writeme DOT com>, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: q: va_list and va_start?

At 08:26  10/11/1997 +0200, Jose Luis Perandones Colino wrote:
>How works va_list,va_start and ... arguments????
It's somewhat complicated, but basically:
* You declare a variable of type va_list.
* You pass it to va_start with the name of the last fixed argument.
* You call va_arg with the type of the argument you want.
* You call va_end.

Here is an example which I stole from the glibc docs:

     #include <stdarg.h>
     #include <stdio.h>
     
     int
     add_em_up (int count,...)
     {
       va_list ap;
       int i, sum;
     
       va_start (ap, count);         /* Initialize the argument list. */
     
       sum = 0;
       for (i = 0; i < count; i++)
         sum += va_arg (ap, int);    /* Get the next argument value. */
     
       va_end (ap);                  /* Clean up. */
       return sum;
     }
     
     int
     main (void)
     {
       /* This call prints 16. */
       printf ("%d\n", add_em_up (3, 5, 5, 6));
     
       /* This call prints 55. */
       printf ("%d\n", add_em_up (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
     
       return 0;
     }
Eli, shouldn't these be documented with the DJGPP distribution?

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019