Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2007-11-25
14
Medium Priority
?
218 Views
Last Modified: 2010-04-21
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
Comment
Question by:jonathanjeffrey
  • 6
  • 4
  • 4
14 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20348030
The problem is with 'complexData-1'
Why do you substract 1?
Could you send complexData declaration and use?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20348045
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
 

Author Comment

by:jonathanjeffrey
ID: 20348046
I get the same compile errors even if I dont subtract 1.    Or did you mean something else?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Expert Comment

by:evilrix
ID: 20348054
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
 

Author Comment

by:jonathanjeffrey
ID: 20348084
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20348092
How did you define TestFunction_t?
I think the problem could be there...
0
 

Author Comment

by:jonathanjeffrey
ID: 20348105
typedef void (*TestFunction_t)();
typedef double (*ExecTestFunction_t)(TestFunction_t);
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20348112
it should be:
typedef void (*TestFunction_t)(double [], int, int);
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20348118
>> 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
 
LVL 40

Accepted Solution

by:
evilrix earned 1400 total points
ID: 20348125
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20348128
When I say working -- I mean building :)
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 600 total points
ID: 20348132
@evilrix,
>>typedef void (*TestFunction_t)(double [], int, int);
I have stated this. My proxy is working well today ;)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20348141
>> 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
 

Author Closing Comment

by:jonathanjeffrey
ID: 31410914
Thanks, was able to get it working from that.  
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question