简体   繁体   English

C —函数指针数组

[英]C — Array of Function Pointers

I am using C and looking to populate an array with pointers to various functions. 我正在使用C,希望用指向各种函数的指针填充数组。 I would like to use a function to pass the address of the function I want to put in the array. 我想使用一个函数来传递要放入数组中的函数的地址。 This is what I am trying to do with no success. 这是我试图做的没有成功的事情。 The compiler is barking at my "task *ptr" deceleration in the prototype and registerTask function definition. 编译器对原型和registerTask函数定义中的“ task * ptr”减速度产生了咆哮。 I see a lot of examples of arrays with pointers to functions but none that first pass the address of the function through a function argument and then update the array. 我看到了很多带有指向函数的指针的数组的示例,但没有一个示例首先通过函数参数传递函数的地址,然后更新数组。 Any suggestions welcome. 任何建议欢迎。

typedef void (*task)(void);
task TaskArray[32];

// Prototypes
void taskA(void);
void taskB(void);
void registerTask(task *ptr);

// This is the problem function definition
void registerTask(task *ptr) {

    TaskArray[TaskIndex] = ptr;
    ++TaskIndex;

}

// Some example functions definitions
void taskA(void) { }   
void taskB(void) { }

int TaskIndex = 0;

main {

    registerTask(&taskA);       // place taskA pointer in array
    registerTask(&taskB);       // place taskB pointer in array

    ...later on...

    TaskIndex = value;          // Set the Task Array function Index
    (*TaskArray[TaskIndex]);    // call the function

}

task is a function pointer. task 一个函数指针。 Therefore, registerTask doesn't need to take a task *ptr parameter (which would be a function pointer pointer), just a task ptr . 因此, registerTask不需要带一个task *ptr参数(它将是一个函数指针指针),只需一个task ptr

Also, your syntax for calling the function is incorrect; 另外,您调用该函数的语法不正确; you need the open and close parenthesis, just as if you were calling a regular ol' function. 您需要打开和关闭括号,就像调用常规ol'函数一样。

Finally, you don't need the ampersand in front of the function names whose address you're taking - the function name is sufficient. 最后,您不需要在要使用其地址的函数名称前使用与号-函数名称已足够。 And, you don't need to dereference the function pointer when calling it. 并且,在调用函数指针时无需取消引用。

typedef void (*task)(void);
task TaskArray[32];

// Prototypes
void taskA(void);
void taskB(void);
void registerTask(task taskPtr);

void registerTask(task taskPtr) {

    TaskArray[TaskIndex] = taskPtr;
    ++TaskIndex;

}

// Some example functions definitions
void taskA(void) { }   
void taskB(void) { }

int TaskIndex = 0;

main {

    registerTask(taskA);       // place taskA pointer in array
    registerTask(taskB);       // place taskB pointer in array

    //...later on...

    TaskIndex = value;          // Set the Task Array function Index
    TaskArray[TaskIndex]();     // call the function

}

I have made a few changes so the code should compile and mostly work. 我做了一些更改,以便代码可以编译并且可以正常工作。 The changes are: 更改为:

1) move the scope of TaskIndex so it can be seen by registerTask(). 1)移动TaskIndex的范围,以便可以通过registerTask()看到它。
2) remove a lot of extra indirection 2)删除很多额外的间接
3) fixed the function pointer call 3)修复了函数指针的调用

typedef void (*task)(void);
task TaskArray[32];

// Prototypes
void taskA(void);
void taskB(void);

int TaskIndex = 0;

// This is the problem function definition
void registerTask(task ptr) {
    TaskArray[TaskIndex++] = ptr;
}

// Some example functions definitions
void taskA(void) { }   
void taskB(void) { }

int
main (void)
{

    registerTask(taskA);       // place taskA pointer in array
    registerTask(taskB);       // place taskB pointer in array

    ...later on...

    TaskIndex = value;          // Set the Task Array function Index
    TaskArray[TaskIndex]();    // call the function

}

IMHO things are a lot simpler if you don't use pointer typedefs. 如果您不使用指针typedef,恕我直言,事情要简单得多。 And if you use some sort of naming convention so that types are different to instances of that type. 并且,如果您使用某种命名约定,则类型不同于该类型的实例。 For example: 例如:

typedef void TASK(void);

// Prototypes
TASK taskA, taskB;

TASK *TaskArray[32];
int NumTasks = 0;

void registerTask(TASK *ptr)
{
    TaskArray[NumTasks++] = ptr;
}

// Some example functions definitions
void taskA(void) { }   
void taskB(void) { }

main {
    registerTask(&taskA);       // place taskA pointer in array
    registerTask(&taskB);       // place taskB pointer in array

// call the function
    TaskArray[0]();
}

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

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