英文:
Array of functions in C, with void pointer argument, dinamically alocated
问题
我无法声明如何正确声明一个可以作为参数传递给get_operations函数的函数向量,然后我可以使用我之前声明的向量的组件来调用这8个函数。以下是我使用的结构和函数:
void get_operations(void **operations) {
	operations[0] = tire_pressure_status;
	operations[1] = tire_temperature_status;
	operations[2] = tire_wear_level_status;
	operations[3] = tire_performance_score;
	operations[4] = pmu_compute_power;
	operations[5] = pmu_regenerate_energy;
	operations[6] = pmu_get_energy_usage;
	operations[7] = pmu_is_battery_healthy;
}
请注意,这是您提供的代码段的翻译部分。
英文:
I can't declare how to properly declare a function vector that I can pass as an argument to the get_operations function, then I can call the 8 functions with the help of the components of my previously declared vector. Here is the structures I used and the functions:
void get_operations(void **operations) {
	operations[0] = tire_pressure_status;
	operations[1] = tire_temperature_status;
	operations[2] = tire_wear_level_status;
	operations[3] = tire_performance_score;
	operations[4] = pmu_compute_power;
	operations[5] = pmu_regenerate_energy;
	operations[6] = pmu_get_energy_usage;
	operations[7] = pmu_is_battery_healthy;
}
答案1
得分: 1
以下是您要翻译的代码部分:
typedef int (*statfunc)(void *);
int tire_pressure_status(void *);
int tire_temperature_status(void *);
int tire_wear_level_status(void *);
int tire_performance_score(void *);
void get_operations(void **operations) {
    statfunc *vect = *operations;
    vect[0] = tire_pressure_status;
    vect[1] = tire_temperature_status;
    vect[2] = tire_wear_level_status;
    vect[3] = tire_performance_score;
}
int main(void) {
    statfunc vector[4];
    void *ptr_to_vector = vector;
    get_operations(&ptr_to_vector);
    vector[0](NULL);
    vector[1](NULL);
    vector[2](NULL);
    vector[3](NULL);
}
int tire_pressure_status(void *) {
    return printf("%s\n", __func__);
}
int tire_temperature_status(void *) {
    return printf("%s\n", __func__);
}
int tire_wear_level_status(void *) {
    return printf("%s\n", __func__);
}
int tire_performance_score(void *) {
    return printf("%s\n", __func__);
}
您提供的代码已经被翻译成中文,不包括不需要翻译的部分。
英文:
typedef int (*statfunc)(void *);
int tire_pressure_status(void *);
int tire_temperature_status(void *);
int tire_wear_level_status(void *);
int tire_performance_score(void *);
void get_operations(void **operations) {
    statfunc *vect = *operations;
    vect[0] = tire_pressure_status;
    vect[1] = tire_temperature_status;
    vect[2] = tire_wear_level_status;
    vect[3] = tire_performance_score;
}
int main(void)
{
    statfunc vector[4];
    void *ptr_to_vector = vector;
    get_operations(&ptr_to_vector);
    vector[0](NULL);
    vector[1](NULL);
    vector[2](NULL);
    vector[3](NULL);
}
int tire_pressure_status(void *)
{
    return printf("%s\n", __func__);
}
int tire_temperature_status(void *)
{
    return printf("%s\n", __func__);
}
int tire_wear_level_status(void *)
{
    return printf("%s\n", __func__);
}
int tire_performance_score(void *)
{
    return printf("%s\n", __func__);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论