/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
#include <stdarg.h>
#include <array.h>

void fun (int k, ...) {
    int m, n, vacount, num;
    ChType_t dtype;
    void *vptr;
    va_list ap;

    va_start(ap, k);
    vacount = va_count(ap);
    printf("va_count(ap) = %d\n", vacount);
    if(va_arraytype(ap)==CH_CARRAYTYPE ||
       va_arraytype(ap)==CH_CHARRAYTYPE) {
      printf("va_arraydim(ap)= %d\n", va_arraydim(ap));
      num = va_arraynum(ap);
      printf("va_arraynum(ap)= %d\n", num);
      m = va_arrayextent(ap, 0);
      printf("va_arrayextent(ap, 0)= %d\n", m);
      if(va_arraydim(ap) > 1) {
        n = va_arrayextent(ap, 1);
        printf("va_arrayextent(ap, 1)= %d\n", n);
      }
      if(va_datatype(ap) == CH_INTTYPE) {
        int a[num], *p; 
        dtype = va_datatype(ap);
        vptr = va_arg(ap, void *);
        printf("array element is int\n");
        p = vptr;
        printf("p[0] = %d\n", p[0]);
        arraycopy(a, CH_INTTYPE, vptr, dtype, num);
        printf("a[0] = %d\n", a[0]);
      }
      else if(va_datatype(ap) == CH_DOUBLETYPE) {
        array double b[m][n];
        dtype = va_datatype(ap);
        vptr = va_arg(ap, void *);
        printf("array element is double\n");
        arraycopy(&b[0][0], CH_DOUBLETYPE, vptr, dtype, num);
        printf("b = \n%f", b);
      }
    }
    else  if(va_datatype(ap) == CH_INTTYPE)
      printf("data type is int\n");
    else  if(va_datatype(ap) == CH_DOUBLETYPE)
      printf("data type is double\n");
    else  if(va_datatype(ap) == CH_INTPTRTYPE)
      printf("data type is pointer to int\n");
    va_end(ap);
}

int main() {
   int i, a[4]={10, 20, 30}, *p; 
   array double b[2][3]={1, 2, 3, 4, 5, 6};

   p = &i;
   fun(i, a);
   fun(i, b,a);
   fun(i, p);
}
