Newsgroup: it.comp.lang.c
Titolo: Numero di parametri di una funzione

Ho già postato il messaggio ed ho ricevuto risposte, ma incomincio ad
irritarmi......... ;-))
Frequenti Ing. Informatica, il mio prof. continua a dire che il C non ha il
"supporto diretto" per funzioni con numero di parametri variabili, e che
funzioni come printf,scanf,ecc..... sono state scritte in assembler e
vengono linkate al momento della compilazione.
Mi avete (EMG & AL) già risposto che questo non è vero, ma dove posso
trovare della documentazione ufficiale ?
Sono andato dal mio prof. a ripetere quello che mi avevate scritto senza
travisazioni, ma ha risposto che non è assolutamente vero quello che dite.
La soluzione?
--
________________________________
Togliete NoSpaM per rispondermi
bye Brain

> La soluzione?
Un libro sul C? Magari quello degli autori, K&R.
Altrimenti lo standard ufficiale.
Oppure ancora uno dei molti siti, se ritenuti affidabili dal prof.
--
Alessandro Monti

 wrote in message
news:83onrt$cec$1...@nslave2.tin.it...


>La soluzione?
Stampagli il file stdarg.h. E magari pure l'help del compilatore.
Marco

 La soluzione?
Cambiare scuola?
EMG

trovare della documentazione ufficiale ?
Grazie della fiducia ! ;)
Al tuo professore (in che mani siamo !!!) mostra il seguente codice di
"printf.c" :
/* 
Copyright (C) 1991, 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB.  If
not,write to the Free Software Foundation, Inc., 59 Temple Place -
Suite 330, Boston, MA 02111-1307, USA.  
*/
#include <stdarg.h>
#include <stdio.h>
/* Write formatted output to stdout from the format string FORMAT.  */
/* VARARGS1 */
int
printf (const char *format, ...)
{
  va_list arg;
  int done;
  va_start (arg, format);
  done = vfprintf (stdout, format, arg);
  va_end (arg);
  return done;
}
#ifdef USE_IN_LIBIO
# undef _IO_printf
/* This is for libg++.  */
strong_alias (printf, _IO_printf);
#endif
Il riferimento ufficiale lo trovi alla sezione 7.15 del "Committee
Draft", datato 3 Agosto 1998 :
       7.15  Variable arguments <stdarg.h>
       [#1] The header <stdarg.h> declares a type and defines  four
       macros,  for  advancing  through  a  list of arguments whose
       number and types are not known to the called  function  when
       it is translated.
       [#2]  A  function  may  be  called with a variable number of
       arguments of varying types.   As  described  in  6.9.1,  its
       parameter   list  contains  one  or  more  parameters.   The
       rightmost parameter plays  a  special  role  in  the  access
       mechanism, and will be designated parmN in this description.
       [#3] The type declared is
               va_list
       which is an object type  suitable  for  holding  information
       needed  by the macros va_start, va_arg, va_end, and va_copy.
       If access to the varying arguments is  desired,  the  called
       function  shall declare an object (referred to as ap in this
       subclause) having type va_list.  The object ap may be passed
       as an argument to another function; if that function invokes
       the va_arg macro with parameter ap, the value of ap  in  the
       calling function is indeterminate and shall be passed to the
       va_end macro prior to any further reference to ap.198)
       7.15.1  Variable argument list access macros
       [#1]  The  va_start, va_arg, and va_copy macros described in
       this  subclause  shall  be  implemented   as   macros,   not
       functions.   It  is unspecified whether va_end is a macro or
       an identifier declared with external linkage.   If  a  macro
       definition  is  suppressed  in  order  to  access  an actual
       function, or a program defines an external  identifier  with
       the name va_end, the behavior is undefined.  Each invocation
       of the va_start or va_copy macros  shall  be  matched  by  a
       corresponding invocation of the va_end macro in the function
       accepting a varying number of arguments.
       7.15.1.1  The va_arg macro
       Synopsis
       [#1]
               #include <stdarg.h>
               type va_arg(va_list ap, type);
       Description
       [#2] The va_arg macro expands to an expression that has  the
       specified  type  and  the  value of the next argument in the
       call.  The parameter ap shall be the same as the va_list  ap
       initialized by va_start.  Each invocation of va_arg modifies
       ap so that the values of successive arguments  are  returned
       in  turn.  The parameter type shall be a type name specified
       such that the type of a pointer to an object  that  has  the
       specified  type  can be obtained simply by postfixing a * to
       type.  If there is no actual next argument, or  if  type  is
       not compatible with the type of the actual next argument (as
       promoted according to the default argument promotions),  the
       behavior is undefined, except for the following cases:
         -- one  type  is  a signed integer type, the other type is
            the corresponding unsigned integer type, and the  value
            is representable in both types;
         -- one  type is pointer to void and the other is a pointer
            to a character type.
       Returns
       [#3] The first invocation of the va_arg macro after that  of
       the  va_start  macro returns the value of the argument after
       that specified by parmN.  Successive invocations return  the
       values of the remaining arguments in succession.
       7.15.1.2  The va_copy macro
       Synopsis
       [#1]
               #include <stdarg.h>
               void va_copy(va_list dest, va_list src);
       Description
       [#2]  The  va_copy macro makes the va_list dest be a copy of
       the va_list src, as if the va_start macro had  been  applied
       to  it  followed  by the same sequence of uses of the va_arg
       macro as had previously been used to reach the present state
       of src.
       Returns
       [#3] The va_copy macro returns no value.
       7.15.1.3  The va_end macro
       Synopsis
       [#1]
               #include <stdarg.h>
               void va_end(va_list ap);
       Description
       [#2]  The  va_end macro facilitates a normal return from the
       function whose variable argument list was referred to by the
       expansion  of va_start that initialized the va_list ap.  The
       va_end macro may modify ap so that it is  no  longer  usable
       (without  an  intervening invocation of va_start).  If there
       is no corresponding invocation of the va_start macro, or  if
       the  va_end  macro  is  not  invoked  before the return, the
       behavior is undefined.
       Returns
       [#3] The va_end macro returns no value.
       7.15.1.4  The va_start macro
       Synopsis
       [#1]
               #include <stdarg.h>
               void va_start(va_list ap, parmN);
       Description
       [#2] The va_start macro shall be invoked before  any  access
       to the unnamed arguments.
       [#3] The va_start macro initializes ap for subsequent use by
       va_arg and va_end.  va_start shall not be invoked again  for
       the  same ap without an intervening invocation of va_end for
       the same ap.
       [#4] The parameter parmN is the identifier of the  rightmost
       parameter  in  the  variable  parameter list in the function
       definition  (the  one  just  before  the  ,  ...).   If  the
       parameter parmN is declared with the register storage class,
       with a function or array type, or with a type  that  is  not
       compatible  with  the type that results after application of
       the default argument promotions, the behavior is  undefined.
       Returns
       [#5] The va_start macro returns no value.
       [#6]  EXAMPLE  The  function f1 gathers into an array a list
       of arguments that are pointers to strings (but not more than
       MAXARGS  arguments),  then  passes  the  array  as  a single
       argument  to  function  f2.   The  number  of  pointers   is
       specified by the first argument to f1.
               #include <stdarg.h>
               #define MAXARGS 31
               void f1(int n_ptrs, ...)
               {
                       va_list ap;
                       char *array[MAXARGS];
                       int ptr_no = 0;
                       if (n_ptrs > MAXARGS)
                               n_ptrs = MAXARGS;
                       va_start(ap, n_ptrs);
                       while (ptr_no < n_ptrs)
                               array[ptr_no++] = va_arg(ap, char *);
                       va_end(ap);
                       f2(n_ptrs, array);
               }
       Each  call  to  f1  shall have visible the definition of the
       function or a declaration such as
               void f1(int, ...);
       [#7] The function f3 is similar, but saves the status of the
       variable   argument  list  after  the  indicated  number  of
       arguments; after f2 has been  called  once  with  the  whole
       list,  the  trailing  part of the list is gathered again and
       passed to function f4.
               #include <stdarg.h>
               #define MAXARGS 31
               void f3(int n_ptrs, int f4_after, ...)
               {
                       va_list ap, ap_save;
                       char *array[MAXARGS];
                       int ptr_no = 0;
                       if (n_ptrs > MAXARGS)
                               n_ptrs = MAXARGS;
                       va_start(ap, n_ptrs);
                       while (ptr_no < n_ptrs) {
                               array[ptr_no++] = va_arg(ap, char *);
                               if (ptr_no == f4_after)
                                       va_copy(ap_save, ap);
                       }
                       va_end(ap);
                       f2(n_ptrs, array);
                       // Now process the saved copy.
                       n_ptrs -= f4_after;
                       ptr_no = 0;
                       while (ptr_no < n_ptrs)
                         array[ptr_no++] = va_arg(ap_save, char *);
                       va_end(ap_save);
                       f4(n_ptrs, array);
               }
--
Andrea Laforgia
andrea_...@bigfoot.com