Re: is it possible to have functions with 0, 1, or 2 args?

Liste des GroupesRevenir à cl c  
Sujet : Re: is it possible to have functions with 0, 1, or 2 args?
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.c
Date : 05. Aug 2024, 10:06:42
Autres entêtes
Organisation : Stefan Ram
Message-ID : <lhbj12Fmur8U1@mid.uni-berlin.de>
References : 1
Mark Summerfield <mark@qtrac.eu> wrote or quoted:
The rational is that I'd like to create a function `new_thing()` which
takes an optional size, e.g.,

  I'm hella stoked on writing code that's chill in C. So,
  in a sitch like this, I might roll with a function that's
  /always/ down to take an argument and just hit it with a
  "-1" if I'm not trying to lock down a size.

  The "va_list" jazz in C, courtesy of the <stdarg.h> header, lets
  functions roll with a flexible number of args. It's like a buffet
  line for arguments, where you can scoop them up one by one using
  those gnarly macros "va_start", "va_arg", and "va_end".

  But here's the rub - the function's not psychic, so it can't just
  divine how many args are coming down the pike. You got to clue it in
  somehow, like passing a head count or dropping a hint about where to
  pump the brakes. The coder doesn't have to play bean counter though;
  he could just slap a "Dead End" sign at the tail of the arg parade!

  main.c

#include <stdio.h>
#include <stdarg.h>

int f( int const n, ... )
{ va_list args; /* to hold the variable arguments */
  va_start( args, n ); /* Initialize the "va_list" variable with the */
  int result;          /* variable arguments, starting after n       */
  switch( n )
  { case 0: result = f( 2, 3, 11 ); break;
    /* va_arg(args, int) retrieves the next argument from the "va_list"
       of type "int" */
    case 1: result = f( 2, 3, va_arg( args, int )); break;
    case 2:
    result = va_arg( args, int )+ va_arg( args, int ); break; }
  va_end( args ); /* Clean up the "va_list" */
  return result; }

int main( void )
{ printf
  ( "%d\n", 14 == f( 0 )&& 8 == f( 1, 5 )&& 11 == f( 2, 2, 9 )); }

  transcript

1

Date Sujet#  Auteur
5 Aug 24 * is it possible to have functions with 0, 1, or 2 args?15Mark Summerfield
5 Aug 24 +- Re: is it possible to have functions with 0, 1, or 2 args?1Stefan Ram
5 Aug 24 +* Re: is it possible to have functions with 0, 1, or 2 args?4Stefan Ram
5 Aug 24 i+* Re: is it possible to have functions with 0, 1, or 2 args?2Stefan Ram
8 Aug 24 ii`- Re: is it possible to have functions with 0, 1, or 2 args?1Lawrence D'Oliveiro
11 Aug 24 i`- Re: is it possible to have functions with 0, 1, or 2 args?1Tim Rentsch
5 Aug 24 +* Re: is it possible to have functions with 0, 1, or 2 args?6Michael S
12 Aug 24 i`* Re: is it possible to have functions with 0, 1, or 2 args?5Tim Rentsch
12 Aug 24 i `* Re: is it possible to have functions with 0, 1, or 2 args?4Richard Harnden
12 Aug 24 i  `* Re: is it possible to have functions with 0, 1, or 2 args?3Tim Rentsch
12 Aug 24 i   `* Re: is it possible to have functions with 0, 1, or 2 args?2Richard Harnden
13 Aug 24 i    `- Re: is it possible to have functions with 0, 1, or 2 args?1Tim Rentsch
5 Aug 24 +- Re: is it possible to have functions with 0, 1, or 2 args?1Stefan Ram
5 Aug 24 +- Re: is it possible to have functions with 0, 1, or 2 args?1Blue-Maned_Hawk
12 Aug 24 `- Re: is it possible to have functions with 0, 1, or 2 args?1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal