C中的函数数组,带有void指针参数,动态分配。

huangapple go评论57阅读模式
英文:

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__);
}

https://godbolt.org/z/1nTPKd8v1

huangapple
  • 本文由 发表于 2023年4月7日 03:12:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定