【问题标题】:Pass any struct member to function将任何结构成员传递给函数
【发布时间】:2021-03-03 20:20:38
【问题描述】:

我有以下结构:

typedef struct
{
    uint8_t number;
    uint8_t user_bank;
    uint8_t temperature;
    float acc_xyz[3];
    float gyro_xyz[3];
    float mag_xyz[3];
    float acc_bias[3];
    float gyro_bias[3];
    float quaternions[4];
    char *orientation_of_IMU;
    char *component_orientation_on_pcb;
} Sensor;

我有兴趣将一些浮点成员表示为字节,因此我可以通过 SPI 传输它们。到目前为止,我有这些函数,它们允许我包装 4 个浮点四元数,并在缓冲区中返回 16 个字节。

typedef char byte;
void floatToByteArray(float f, byte buf[4])
{
    memcpy(buf, &f, sizeof(f));
}

//Function that takes float quaternions from a sensor struct, and returns their values, represented as bytes for easier SPI transfer.

void wrap_quaternions(Sensor* imu, int8_t *buff)
{
    for (int i = 0; i < 4; i++)
    {
        floatToByteArray(imu->quaternions[i], &buff[4*i]);
    }
}

现在我对泛化它很感兴趣,因此我可以在 Sensor 结构中获取我的任何浮点数组成员,并相应地包装它们。我希望能够使用参数为 imu->float_member 调用 floatToByteArray 函数,但我不能这样做。然后我可以将数字 4 更改为 sizeof(float_member)。

有解决办法吗?

【问题讨论】:

  • 将Sensor* imu 参数替换为int cnt, float *arr[cnt]。然后你可以传递例如3, sensor-&gt;gyro_bias,或者你可以传递sizeof(sensor-&gt;gyro_bias)/sizeof(float), sensor-&gt;gyro_bias。
  • 太棒了!我将添加它,并在我的传感器结构中为包装数据创建一个嵌套结构。然后我将使用参数 sizeof(sensor-&gt;acc_bias), sensor -&gt;acc_bias, sensor -&gt; wrapped_data -&gt; acc_bias_wrapped 。这应该可行,对吧?
  • 嗯。我试过了,但它似乎不起作用。也许您可以查看我的其他评论?
  • 哎呀,对不起,我搞砸了我的例子。 float *arr[cnt] 应该只是参数列表中的float arr[cnt]。

标签: c function pointers struct


【解决方案1】:

我尝试实现 Toms 的想法,但我似乎仍然遇到错误,因为该程序没有按应有的方式运行。也许你们中的一个可以提供帮助。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
 
typedef char byte;
void floatToByteArray(float f, byte buf[4])
{
    memcpy(buf, &f, sizeof(f));
}
 
 void wrap_floatData(int cnt, float *arr[cnt], int8_t *buff)
{
    for (int i = 0; i < cnt; i++)
    {
        floatToByteArray(*arr[i], &buff[cnt*i]);

    }
}


struct Sensor_Data_Wrapped
{
    int8_t quaternions_wrapped[16];
    int8_t mag_xyz_wrapped[12];
    int8_t acc_bias_wrapped[12];
    int8_t gyro_bias_wrapped[12];
};

typedef struct
{
    uint8_t number;
    uint8_t user_bank;
    uint8_t temperature;
    float acc_xyz[3];
    float gyro_xyz[3];
    float mag_xyz[3];
    float acc_bias[3];
    float gyro_bias[3];
    float quaternions[4];
    char *orientation_of_IMU;
    char *component_orientation_on_pcb;
    //struct Sensor_Data_Wrapped wrapped_data;
} Sensor;


Sensor IMU1;
Sensor IMU2; 
int8_t Wrapped_IMU1_bias[12];
 
int main(void) 
{
    
    Wrapped_IMU1_bias[1] = 25;
    
    IMU1.gyro_bias[0] =119.0;
    IMU1.gyro_bias[1] = 1349;
    IMU1.gyro_bias[2] = 5.2;


    
    wrap_floatData(3, IMU1.gyro_bias, Wrapped_IMU1_bias);

    
    for (int i = 0; i < 12; i++)
    {
                printf("Byte is %d", Wrapped_IMU1_bias[i]);
                printf("\n");

    }

return 0; 
    
}

错误是:

main.c:89:20:警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“wrap_floatData”的参数 2 main.c:39:7:注意:预期为“float **”,但参数类型为“float *”分段错误(核心转储)

【讨论】:

  • 哎呀,对不起,我搞砸了我的例子。 float *arr[cnt] 应该只是参数列表中的float arr[cnt]。
  • 不错。现在一切都好。我看到 sizeof 函数给了我 12,因为数组由 3 个浮点数(12 个字节)组成。我已经定义了我自己的 N_elements 函数N_ELEMS(x) (sizeof(x) / sizeof((x)[0])),它给了我想要的结果。感谢您的帮助!
【解决方案2】:

您是否尝试过将浮点参数用作指针?对于这种情况,指针非常方便。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多