Solved

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

Posted on 2007-11-25
14
203 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now