Search
 
SCRIPT & CODE EXAMPLE
 

C

c va_list example

void example_func(int var, ...)
{
	//the variable
	va_list va;
  	//initalizing the arguments list with the first parametter
  	va_start(va, var);
  
  	//read a parametter (list, type)
  	//each call of va_arg will give next argument, but type must be
	//correctly specified otherwise the behaviour is unpredictable
  	// Don't forget type promotion!!! (e.g.: char -> int)
  	va_arg(va, int);
  	//You can also send the list once initialized to another function:
  	exemple_func2(&va);
  	//destroying the list
  	va_end(va);
}

void example(va_list *va)
{
  //No need to initialize / destroy the list, just get the args with
  va_list(*va, int);
}
Comment

va_list in C

// C program to demonstrate working of 
// variable arguments to find average
// of multiple numbers.
#include <stdarg.h>
#include <stdio.h>
  
int average(int num, ...)
{
    va_list valist;
  
    int sum = 0, i;
  
    va_start(valist, num);
    for (i = 0; i < num; i++) 
        sum += va_arg(valist, int);
  
    va_end(valist);
  
    return sum / num;
}
  
// Driver code
int main()
{
    printf("Average of {2, 3, 4} = %d
",
                         average(2, 3, 4));
    printf("Average of {3, 5, 10, 15} = %d
",
                      average(3, 5, 10, 15));
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: string if statements c 
C :: format bool c 
C :: c define array size 
C :: c assign pointer to struct 
C :: c style array 
C :: %d in c 
C :: addition in c 
C :: c check if char is an operator 
C :: sequelize count multiple associations 
C :: c syntax 
C :: c if 
C :: Counting Sort C 
C :: append to list in c 
C :: c string to int 
C :: binary to decimal in c 
C :: add_to_cart how to call it woocommerce 
C :: lateinit kotlin 
C :: simple bootstrap form example 
C :: c check if character is an alphabet 
C :: printf("%3d ",XX); 
C :: set the nth bit 
C :: C strlen implementation 
C :: pasar a binario recursivo 
C :: looping through an array in c 
C :: vifm preview images 
C :: open a file in from terminal 
C :: objects in oops 
C :: c get pid 
C :: obstacle avoiding robot in c++ program 
C :: how to convert c program in to assembly 8051 online 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =