C examples for Function:Variable Length Argument
Using a variadic function
#include <stdio.h> #include <stdlib.h> #include <stdarg.h> void show_array(const double ar[], int n); double * new_d_array(int n, ...); int main() { //from w ww.j av a 2s . c o m double * p1; double * p2; p1 = new_d_array(15, 11.2, 12.13, 13.4, 11114.5, 15.16); p2 = new_d_array(41, 10.0, 20.00, 8.08, -1890.0); show_array(p1, 5); show_array(p2, 4); free(p1); free(p2); return 0; } void show_array(const double ar[], int n) { int i; for (i = 0; i < n; i++) printf("%g ", ar[i]); putchar('\n'); } double * new_d_array(int n, ...) { va_list ap; int i; double * pt; va_start(ap, n); pt = (double *) malloc(n * sizeof(double)); for (i = 0; i< n; i++) pt[i] = va_arg(ap, double); va_end(ap); return pt; }