简体   繁体   English

将一个函数作为参数传递给其他函数

[英]Passing a function as argument to other function

I have been reading about this theme. 我一直在阅读关于这个主题的文章。 I have readed a lot of possible solutions, so please, dont mark my question as duplicated, only need a puntual solution of this problem. 我已经阅读了很多可能的解决方案,所以请不要将我的问题标记为重复,只需要解决该问题即可。

I have a function which calculate time of execution of some code. 我有一个函数,可以计算某些代码的执行时间。 This code will be sent as argument (will be a function). 此代码将作为参数发送(将是一个函数)。

This is the function which calculate the time: 这是计算时间的函数:

double executionTime( /* HERE I WANNA PASS THE FUNCTION TO CALCULATE EXECTIME*/ )
{
    LARGE_INTEGER frequency;
    LARGE_INTEGER start;
    LARGE_INTEGER end;
    double interval;

    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&start);

    // HERE GOES CODE TO PROCCESS

    QueryPerformanceCounter(&end);
    interval = (double) (end.QuadPart - start.QuadPart) / frequency.QuadPart;

    return (interval);
}

I have tryed this (and another ways, but it is the most visible): 我已经尝试过这种方法(还有其他方法,但是最明显):

double executionTime( void (*f)() )
{
    LARGE_INTEGER frequency;
    LARGE_INTEGER start;
    LARGE_INTEGER end;
    double interval;

    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&start);

    // Function to proccess
    f();

    QueryPerformanceCounter(&end);
    interval = (double) (end.QuadPart - start.QuadPart) / frequency.QuadPart;

    return (interval);
}

I do not know if arguments of function to proccess are important. 我不知道处理函数的参数是否重要。 In some sites said yes, in another said no. 在某些网站上说是,在其他网站上说不。 The declaration of the function that I wanna proccess is: 我要处理的函数的声明是:

int readFileAndInsertRegs(char *nombreFichero, PERSONA *tablaHash[], int tam, int tipoInsertado, int tipoPruebaColision) 

I have called function executionTime like: 我将函数的executionTime称为:

executionTime( readFileAndInsertRegs("./files/listaActores.csv", &tablaHash, TAM_E1, NORMAL, LINEAL) );

Can anyone help me with this particular problem? 有人可以帮助我解决这个特定问题吗?

Thank you. 谢谢。

usually the way to do so is to pass function arguments to the executionTime and call the function with them, ie 通常,这样做的方法是将函数参数传递给executionTime并使用它们调用函数,即

double executionTime( void (*f)(), char *arg1, PERSONE arg2[], ... )
{
    // do preamble

     f(arg1, arg2, .....);

    // finish
}

...

executionTime( &readFileAndInsertRegs, "./files/listaActores.csv", &tablaHash, TAM_E1, NORMAL, LINEAL));

here is a working example: 这是一个工作示例:

#include <stdio.h>

void process1(void (*f)(), int farg) {
  f(farg);
}

void f1(int arg) {
  printf("f1: %d\n", arg);
}

int main() {
  process1(&f1, 10);
  return 0;
}

and, in 'c' you do not need to declare all arguments of the function, though you can, and it is a good idea to do so. 并且,尽管可以,但在'c'中无需声明函数的所有参数,这样做是个好主意。 Compiler could do an additional checking then 编译器可以再进行一次检查

    void process1(void (*f)(int), int farg) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM