将一个函数作为参数传递给其他函数 [英] Passing a function as argument to other function

查看:144
本文介绍了将一个函数作为参数传递给其他函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关这个​​主题的文章.我已经阅读了很多可能的解决方案,所以请不要将我的问题标记为重复,只需要解决该问题即可.

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).

这是计算时间的函数:

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) 

我已经像下面这样调用了函数executionTime:

I have called function executionTime like:

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

有人可以帮助我解决这个特定问题吗?

Can anyone help me with this particular problem?

谢谢.

推荐答案

通常的方法是将函数参数传递给executionTime并使用它们调用函数,即

usually the way to do so is to pass function arguments to the executionTime and call the function with them, i.e.

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

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

    // finish
}

...

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

这是一个有效的示例:

#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;
}

,并且在'c'中,您可以声明函数的所有参数,尽管您可以声明,但是这样做是个好主意.编译器可以再进行一次检查

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. Compiler could do an additional checking then

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

这篇关于将一个函数作为参数传递给其他函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆