Lenguaje C: función va_start (obtener parámetro de la lista de parámetros variables)

Los datos expuestos en este blog, son solo de índole informativo. Por favor realiza siempre una copia de seguridad antes de realizar cualquier cambio en tu proyecto.

Lenguaje C: función va_start
(obtener parámetro de la lista de parámetros variables)

En el lenguaje de programación C, función va_start Inicializa la lista de argumentos variables a la que hace referencia ap. La función va_start debe llamarse antes de usar la función va_arg.

sintaxis

La sintaxis de la función va_start en lenguaje C es:

void va_start(va_list ap, parmN);

parámetro o parámetro

ap lista de argumentos variables. parmN El nombre del último parámetro normal.

devolver los bienes

La función va_start no devuelve nada.

titulo requerido

En C, los archivos de encabezado requeridos por la función va_start son:

#include <stdarg.h>

aplicar para

En C, la función va_start está disponible en las siguientes versiones:

  • ANSI/ISO 9899-1990

ejemplo va_start

/* Example using va_start by TechOnTheNet.com */

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

int add(int n, ...)
{
    /* Define temporary variables */
    va_list list;
    int total;

    /* Initialize total */
    total = 0;

    /* Set where the variable length part of the argument list ends */
    va_start(list, n);

    /* Loop through each argument adding the int values */
    for (int i=0; i < n; i++)
        total = total + va_arg(list, int);

    /* Clean up */
    va_end(list);

    /* Return the calculated total */
    return total;
}

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    int value1, value2, value3;
    int result;

    value1 = 3;
    value2 = 4;
    value3 = 5;

    /* Call the add function  */
    result = add(4, value1, value2, value3);

    /* Display the results of the additon */
    printf("The sum of %d, %d and %d is %dn", value1, value2, value3, result);

    return 0;
}

Cuando se compila y ejecuta, esta aplicación generará:

The sum of 3, 4 and 5 is 12

ver también

Otras funciones de C que vale la pena mencionar cuando se trata de la función va_start:

  • función va_arg
  • función va_end
  • función vfprintf
  • función vprintf
  • función vsprintf