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

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

Array of functions in C, with void pointer argument, dinamically alocated

问题

我无法声明如何正确声明一个可以作为参数传递给get_operations函数的函数向量,然后我可以使用我之前声明的向量的组件来调用这8个函数。以下是我使用的结构和函数:

  1. void get_operations(void **operations) {
  2. operations[0] = tire_pressure_status;
  3. operations[1] = tire_temperature_status;
  4. operations[2] = tire_wear_level_status;
  5. operations[3] = tire_performance_score;
  6. operations[4] = pmu_compute_power;
  7. operations[5] = pmu_regenerate_energy;
  8. operations[6] = pmu_get_energy_usage;
  9. operations[7] = pmu_is_battery_healthy;
  10. }

请注意,这是您提供的代码段的翻译部分。

英文:

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:

  1. void get_operations(void **operations) {
  2. operations[0] = tire_pressure_status;
  3. operations[1] = tire_temperature_status;
  4. operations[2] = tire_wear_level_status;
  5. operations[3] = tire_performance_score;
  6. operations[4] = pmu_compute_power;
  7. operations[5] = pmu_regenerate_energy;
  8. operations[6] = pmu_get_energy_usage;
  9. operations[7] = pmu_is_battery_healthy;
  10. }

答案1

得分: 1

以下是您要翻译的代码部分:

  1. typedef int (*statfunc)(void *);
  2. int tire_pressure_status(void *);
  3. int tire_temperature_status(void *);
  4. int tire_wear_level_status(void *);
  5. int tire_performance_score(void *);
  6. void get_operations(void **operations) {
  7. statfunc *vect = *operations;
  8. vect[0] = tire_pressure_status;
  9. vect[1] = tire_temperature_status;
  10. vect[2] = tire_wear_level_status;
  11. vect[3] = tire_performance_score;
  12. }
  13. int main(void) {
  14. statfunc vector[4];
  15. void *ptr_to_vector = vector;
  16. get_operations(&ptr_to_vector);
  17. vector[0](NULL);
  18. vector[1](NULL);
  19. vector[2](NULL);
  20. vector[3](NULL);
  21. }
  22. int tire_pressure_status(void *) {
  23. return printf("%s\n", __func__);
  24. }
  25. int tire_temperature_status(void *) {
  26. return printf("%s\n", __func__);
  27. }
  28. int tire_wear_level_status(void *) {
  29. return printf("%s\n", __func__);
  30. }
  31. int tire_performance_score(void *) {
  32. return printf("%s\n", __func__);
  33. }

您提供的代码已经被翻译成中文,不包括不需要翻译的部分。

英文:
  1. typedef int (*statfunc)(void *);
  2. int tire_pressure_status(void *);
  3. int tire_temperature_status(void *);
  4. int tire_wear_level_status(void *);
  5. int tire_performance_score(void *);
  6. void get_operations(void **operations) {
  7. statfunc *vect = *operations;
  8. vect[0] = tire_pressure_status;
  9. vect[1] = tire_temperature_status;
  10. vect[2] = tire_wear_level_status;
  11. vect[3] = tire_performance_score;
  12. }
  13. int main(void)
  14. {
  15. statfunc vector[4];
  16. void *ptr_to_vector = vector;
  17. get_operations(&ptr_to_vector);
  18. vector[0](NULL);
  19. vector[1](NULL);
  20. vector[2](NULL);
  21. vector[3](NULL);
  22. }
  23. int tire_pressure_status(void *)
  24. {
  25. return printf("%s\n", __func__);
  26. }
  27. int tire_temperature_status(void *)
  28. {
  29. return printf("%s\n", __func__);
  30. }
  31. int tire_wear_level_status(void *)
  32. {
  33. return printf("%s\n", __func__);
  34. }
  35. int tire_performance_score(void *)
  36. {
  37. return printf("%s\n", __func__);
  38. }

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:

确定