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

int chcallfuncbyaddr(double x[:], double y[:], double (*func)(double)) {
  void *handle, *fptr;
  int retval;

  handle = dlopen("libch.dl", RTLD_LAZY);
  if(handle == NULL) {
       printf("Error: %s(): dlopen(): %s\n", __func__, dlerror());
       return -1;
  }
  fptr = dlsym(handle, "chcallfuncbyaddr_chdl");
  if(fptr == NULL)  {
     printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
     return -1;
  }
  dlrunfun(fptr, &retval, chcallfuncbyaddr, x, y, func);
  if(dlclose(handle)!=0) {
     printf("Error: %s(): dlclose(): %s\n", __func__, dlerror());
     return -1;
  }

  return retval;
}

double  func(double a){
  return a*a;
} 

int main() {
  array double x[4] = {1,2,3,4}, y[4];

  chcallfuncbyaddr(x, y, func);
  printf("x = %f\n", x);
  printf("y = %f\n", y);
}
