Solved

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

Posted on 2007-11-25
14
210 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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 350 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 150 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

830 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