Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Passing a function (and its arguements) as arguemnts to another function

Here is what I am trying to accomplish.  I have a function which implements a FFT.  (called four1, its signature is listed in the code snippet section).  

I also have a function called timeTestFunction_clock (also listed in code snippet) which I want to run the four1 function.  So I pass the address of the four1 function, and its parameters to my timeTestFunction_clock.

What I want to have happen is for the timeTestFunction_clock function to run the function I pass it, and then return how long it takes.  I am having a problem in how I call timeTestFunction_clock though.  

This is how I call it:
double d = timeTestFunction_clock(four1, complexData-1, SIZE, 1);

And this is the complie error I get on the above line of code:
error: invalid conversion from `void (*)(double*, int, int)' to `void (*)()'
error:   initializing argument 1 of `double timeTestFunction_clock(void (*)(), double*,  int, int)'

Could somebody help me fix the way I am calling it (or whatever other mistake I may be making?)

Thanks,
Jonathan

double timeTestFunction_clock(TestFunction_t func, double[] data, int i1, int i2)
{
  clock_t start;
  clock_t end;
 
  start = clock();
  func(data, i1, i2);
  end = clock();
 
  return ((double) (end-start))/CLOCKS_PER_SEC;
}
 
 
The signature of the function four1 is:
d = timeTestFunction_clock(four1, complexData-1, SIZE, 1);

Open in new window

0
jonathanjeffrey
Asked:
jonathanjeffrey
  • 6
  • 4
  • 4
2 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
The problem is with 'complexData-1'
Why do you substract 1?
Could you send complexData declaration and use?
0
 
evilrixSenior Software Engineer (Avast)Commented:
Is the example code you posted complete? You state the function at the bottom is the signature for four1 but it's not -- it's the code calling timeTestFunction_clock(). Can you post the complete code?
0
 
jonathanjeffreyAuthor Commented:
I get the same compile errors even if I dont subtract 1.    Or did you mean something else?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
evilrixSenior Software Engineer (Avast)Commented:
I don't think that's the problem -- I think that's a read herring, subtracting 1 from a point still results in a pointer!
0
 
jonathanjeffreyAuthor Commented:
here is the complete code.
void four1(double data[], int nn, int isign)
{
    unsigned long n, mmax, m, j, istep, i;
    double wtemp, wr, wpr, wpi, wi, theta;
    double tempr, tempi;
 
    n = nn << 1;
    j = 1;
 
    for (i = 1; i < n; i += 2) {
        if (j > i) {
            SWAP(data[j], data[i]);
            SWAP(data[j+1], data[i+1]);
        }
        m = nn;
        while (m >= 2 && j > m) {
            j -= m;
            m >>= 1;
        }
        j += m;
    }
 
    mmax = 2;
    while (n > mmax) {
        istep = mmax << 1;
        theta = isign * (6.28318530717959 / mmax);
        wtemp = sin(0.5 * theta);
        wpr = -2.0 * wtemp * wtemp;
        wpi = sin(theta);
        wr = 1.0;
        wi = 0.0;
        for (m = 1; m < mmax; m += 2) {
            for (i = m; i <= n; i += istep) {
                j = i + mmax;
                tempr = wr * data[j] - wi * data[j+1];
                tempi = wr * data[j+1] + wi * data[j];
                data[j] = data[i] - tempr;
                data[j+1] = data[i+1] - tempi;
                data[i] += tempr;
                data[i+1] += tempi;
            }
            wr = (wtemp = wr) * wpr - wi * wpi + wr;
            wi = wi * wpr + wtemp * wpi + wi;
        }
 mmax = istep;
    }
}
 
 
 
 
*******************************************************************
 
int main()
{
    int i;
 
    double d;
    createComplexSawtooth(complexData, SIZE);
    displayComplex(complexData, SIZE);
    d = timeTestFunction_clock(four1, complexData-1, SIZE, 1);
    //four1(complexData-1, SIZE, 1);  <-- this works
    postProcessComplex(complexData, SIZE);
    return 0;
 
)

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
How did you define TestFunction_t?
I think the problem could be there...
0
 
jonathanjeffreyAuthor Commented:
typedef void (*TestFunction_t)();
typedef double (*ExecTestFunction_t)(TestFunction_t);
0
 
Jaime OlivaresSoftware ArchitectCommented:
it should be:
typedef void (*TestFunction_t)(double [], int, int);
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> double timeTestFunction_clock(TestFunction_t func, double[] data, int i1, int i2)

double timeTestFunction_clock(TestFunction_t func, double data[], int i1, int i2)
0
 
evilrixSenior Software Engineer (Avast)Commented:
I've removed all the function implementation as it wasn't part of the problem... working code: -
void four1(double data[], int nn, int isign)
{
}
 
typedef void (* TestFunction_t)(double [], int, int);
 
double timeTestFunction_clock(TestFunction_t func, double data[], int i1, int i2)
{
  func(data, i1, i2);
  return 0.0;
}
 
int main()
{
    double d = timeTestFunction_clock(four1, 0, 0, 1);
    return 0;
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
When I say working -- I mean building :)
0
 
Jaime OlivaresSoftware ArchitectCommented:
@evilrix,
>>typedef void (*TestFunction_t)(double [], int, int);
I have stated this. My proxy is working well today ;)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> I have stated this. My proxy is working well today ;)
I don't recall stating you didn't! There were other things wrong with the code, I have brought them all together into something that builds!
0
 
jonathanjeffreyAuthor Commented:
Thanks, was able to get it working from that.  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now