OiO.lk Blog C# How can I define a function with structures in C?
C#

How can I define a function with structures in C?


I’m woking on a C program with the following files:

functiontest.h
double funcCmp(double(*func1)(double), double(*func2)(double), 
               double xM, double xI, int xN, enum NormType ntype) {
  double x, y_1, y_2, err;
  int i;
  err = 0; 
  MY_ASSERT(xNum > 0);
  for (i = 0; i < xN; ++i) {
    x = xM + xI * i;
    y_1 = (*func1)(x);
    y_2 = (*func2)(x);
    err = updateErr(err, y_1, y_2, ntype); 
  }
  if (ntype == NORM1|| ntype == NORM2) {
    err = err / xNum;
  }
  return err; 
}

double updateErr(double errPrev, double val1, double val2, enum NormType ntype) {
  …
  return errCurr; 
}
utile.h
long computeElapsedTime(struct timeval start, struct timeval end)
{
  return ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec))/1000.0;
}

and two more files from my textbook (trig.h and trig.c) containing the declaration and definition of the functions cos, sin, and tangent.

When I first compiled, I obtained the following error:

util.h:22:6: error: redefinition computeElapsedTime function

which I fixed by adding the following macro at the beginning of the files utile.h, testfunction.h, and fasttrigo.h.

#ifndef SO_UTILE_H\_
#define SO_UTILE_H\_
..
#endif

However, now I have another error:

conflicting types for ‘updateErr’

which I believe is thrown because the declaration and definition of the functions happen in the same files instead of separate files. However, I’m having a hard time declaring them because I don’t know how to define functions with struct parameters.



You need to sign in to view this answers

Exit mobile version