.\" Copyright (c) 1990, 1991 The Regents of the University of California. .\" All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the American National Standards Committee X3, on Information .\" Processing Systems. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)stdarg.3 6.8 (Berkeley) 6/29/91 .\" .\" Converted for Linux, Mon Nov 29 15:11:11 1993, faith@cs.unc.edu .\" Translation revised Tue Apr 18 2000 by Juan Piernas .\" .TH STDARG 3 "29 noviembre 1993" "BSD MANPAGE" "Manual del Programador de Linux" .SH NOMBRE stdarg \- lista de argumentos variable .SH SINOPSIS .B #include .sp .BI "void va_start( va_list " ap ", " last ); .br .BI "" type " va_arg( va_list " ap ", " type ); .br .BI "void va_end( va_list " ap ); .SH DESCRIPCIÓN Una función podría ser llamada con un número de argumentos variable de tipos igualmente variables. El archivo de cabecera (include) .I stdarg.h declara un tipo .B va_list y define tres macros para moverse a través de una lista de argumentos cuyo número y tipo no son conocidos para la función llamada. .PP Dicha función debe declarar un objeto de tipo .B va_list el cual es utilizado por las macros .BR va_start , .BR va_arg y .BR va_end . .PP La macro .B va_start inicializa .I ap para su uso posterior por .B va_arg y .BR va_end , y debe ser llamada en primer lugar. .PP El parámetro .I last es el nombre del último parámetro antes de la lista de argumentos variables, es decir, el último parametro sobre el cual la función llamada conoce el tipo. .PP Dado que la dirección de este parámetro es utilizada por la macro .B va_start no debería ser declarado como una variable de registro, como una función ni como un array. .PP La macro .B va_start no devuelve valor alguno. .PP La macro .B va_arg expande una expresión que contiene el tipo y el valor del próximo argumento empleado en la llamada. El parámetro .I ap es el .BI va_list " " ap inicializado por .BR va_start . Cada llamada a .B va_arg modifica .I ap por tanto la siguiente llamada devolverá el próximo argumento. El parámetro .I type es el nombre de un tipo especificado para que para que el tipo de un puntero a un objeto que es de dicho tipo pueda ser obtenido simplemente añadiendo un * a .IR type . .PP Si no hay próximo argumento, o si .I type no es compatible con el tipo del próximo argumento, se producieran errores impredecibles. .PP El primer uso de la macro .B va_arg despues de .B va_start devuelve el argumento tras .IR last . Invocaciones sucesivas devolverán los valores del resto de los argumentos. .PP La macro .B va_end manipula un retorno normal de la función cuya lista de argumentos variable fue inicializada por .BR va_start . .PP La macro .B va_end no devuelve valor alguno. .SH EJEMPLOS La función .I foo toma una cadena de caracteres de formato e imprime el argumento asociado con cada caracter de formato basado en el tipo. .RS .nf void foo(char *fmt, ...) { va_list ap; int d; char c, *p, *s; va_start(ap, fmt); while (*fmt) switch(*fmt++) { case 's': /* string */ s = va_arg(ap, char *); printf("string %s\en", s); break; case 'd': /* int */ d = va_arg(ap, int); printf("int %d\en", d); break; case 'c': /* char */ /* Aquí es necesaria una conversión forzada * de tipos ya que va_arg sólo toma tipos * totalmente `ascendidos'. */ c = (char) va_arg(ap, char); printf("char %c\en", c); break; } va_end(ap); } .fi .RE .SH CONFORME A Las macros .BR va_start , .BR va_arg y .B va_end concuerdan con ANSI C3.159-1989 (``ANSI C''). .SH COMPATIBILIDAD Estas macros .I no son compatibles con las macros históricas que reemplazan. Se puede encontrar una versión compatible "hacia atrás" en el fichero de cabecera .IR varargs.h . .SH ERRORES Al contrario que las macros .BR varargs , las macros .B stdarg no permiten a los programadores codificar una función con argumentos variables. Este problema genera trabajo principalmente cuando se convierte código basado en .B varargs a .B stdarg pero además crea dificultades a las funciones que quieran pasar todos sus argumentos en una función que toma una lista de argumentos .BR va_list , como .BR vfprintf (3).