首页 > 解决方案 > 变量值在结构赋值后打印和没有结构赋值时打印时是不同的

问题描述

我不知道如何放一个合适的标题来解释这个问题。因此,如果您有更多信息量的标题要编辑,请随意。

为了理解这个问题,让我解释一下我在做什么。

我创建了一个结构如下:

typedef union __attribute__ ((__packed__)) adcs_measurements_t
{
    unsigned char raw[72];
    struct __attribute__ ((__packed__)) //191
    {
        int magneticFieldX : 16;
        int magneticFieldY : 16;
        int magneticFieldZ : 16;
        int coarseSunX : 16;
        int coarseSunY : 16;
        int coarseSunZ : 16;
        int sunX : 16;
        int sunY : 16;
        int sunZ : 16;
        int nadirX : 16;
        int nadirY : 16;
        int nadirZ : 16;
        int XAngularRate : 16;
        int YAngularRate : 16;
        int ZAngularRate : 16;
        int XWheelSpeed : 16;
        int YWheelSpeed : 16;
        int ZWheelSpeed : 16;
        int star1BX : 16;
        int star1BY : 16;
        int star1BZ : 16;
        int star1OX : 16;
        int star1OY : 16;
        int star1OZ : 16;
        int star2BX : 16;
        int star2BY : 16;
        int star2BZ : 16;
        int star2OX : 16;
        int star2OY : 16;
        int star2OZ : 16;
        int star3BX : 16;
        int star3BY : 16;
        int star3BZ : 16;
        int star3OX : 16;
        int star3OY : 16;
        int star3OZ : 16;
    } fields;
} adcs_measurements_t;

我通过调用如下函数来填充结构:

void adcsTM191_measurements(adcs_measurements_t* dataOut)
{
    int pass;
    unsigned char TMID = 191;
    unsigned char readBuff[72] = {0};
    pass = I2C_write(ADCS_ADDR, &TMID, 1);
    if(pass != 0)
    {
        printf("write error %d\n", pass);
    }
    pass = I2C_read(ADCS_ADDR, readBuff, 72);
    if(pass != 0)
    {
        printf("read error %d\n", pass);
    }

    dataOut->fields.magneticFieldX = (readBuff[1] & 0x00FF) << 8 | (readBuff[0] & 0x00FF);
    dataOut->fields.magneticFieldY = (readBuff[3] & 0x00FF) << 8 | (readBuff[2] & 0x00FF);
    dataOut->fields.magneticFieldZ = (readBuff[5] & 0x00FF) << 8 | (readBuff[4] & 0x00FF);
    dataOut->fields.coarseSunX = (readBuff[7] & 0x00FF) << 8 | (readBuff[6] & 0x00FF);
    dataOut->fields.coarseSunY = (readBuff[9] & 0x00FF) << 8 | (readBuff[8] & 0x00FF);
    dataOut->fields.coarseSunZ = (readBuff[11] & 0x00FF) << 8 | (readBuff[10] & 0x00FF);
    dataOut->fields.sunX = (readBuff[13] & 0x00FF) << 8 | (readBuff[12] & 0x00FF);
    dataOut->fields.sunY = (readBuff[15] & 0x00FF) << 8 | (readBuff[14] & 0x00FF);
    dataOut->fields.sunZ = (readBuff[17] & 0x00FF) << 8 | (readBuff[16] & 0x00FF);
    dataOut->fields.nadirX = (readBuff[19] & 0x00FF) << 8 | (readBuff[18] & 0x00FF);
    dataOut->fields.nadirY = (readBuff[21] & 0x00FF) << 8 | (readBuff[20] & 0x00FF);
    dataOut->fields.nadirZ = (readBuff[23] & 0x00FF) << 8 | (readBuff[22] & 0x00FF);
    dataOut->fields.XAngularRate = (readBuff[25] & 0x00FF) << 8 | (readBuff[24] & 0x00FF);
    dataOut->fields.YAngularRate = (readBuff[27] & 0x00FF) << 8 | (readBuff[26] & 0x00FF);
    dataOut->fields.ZAngularRate = (readBuff[29] & 0x00FF) << 8 | (readBuff[28] & 0x00FF);
    dataOut->fields.XWheelSpeed = (readBuff[31] & 0x00FF) << 8 | (readBuff[30] & 0x00FF);
    dataOut->fields.YWheelSpeed = (readBuff[33] & 0x00FF) << 8 | (readBuff[32] & 0x00FF);
    dataOut->fields.ZWheelSpeed = (readBuff[35] & 0x00FF) << 8 | (readBuff[34] & 0x00FF);
    dataOut->fields.star1BX = (readBuff[37] & 0x00FF) << 8 | (readBuff[36] & 0x00FF);
    dataOut->fields.star1BY = (readBuff[39] & 0x00FF) << 8 | (readBuff[38] & 0x00FF);
    dataOut->fields.star1BZ = (readBuff[41] & 0x00FF) << 8 | (readBuff[40] & 0x00FF);
    dataOut->fields.star1OX = (readBuff[43] & 0x00FF) << 8 | (readBuff[42] & 0x00FF);
    dataOut->fields.star1OY = (readBuff[45] & 0x00FF) << 8 | (readBuff[44] & 0x00FF);
    dataOut->fields.star1OZ = (readBuff[47] & 0x00FF) << 8 | (readBuff[46] & 0x00FF);
    dataOut->fields.star2BX = (readBuff[49] & 0x00FF) << 8 | (readBuff[48] & 0x00FF);
    dataOut->fields.star2BY = (readBuff[51] & 0x00FF) << 8 | (readBuff[50] & 0x00FF);
    dataOut->fields.star2BZ = (readBuff[53] & 0x00FF) << 8 | (readBuff[52] & 0x00FF);
    dataOut->fields.star2OX = (readBuff[55] & 0x00FF) << 8 | (readBuff[54] & 0x00FF);
    dataOut->fields.star2OY = (readBuff[57] & 0x00FF) << 8 | (readBuff[56] & 0x00FF);
    dataOut->fields.star2OZ = (readBuff[59] & 0x00FF) << 8 | (readBuff[58] & 0x00FF);
    dataOut->fields.star3BX = (readBuff[61] & 0x00FF) << 8 | (readBuff[60] & 0x00FF);
    dataOut->fields.star3BY = (readBuff[63] & 0x00FF) << 8 | (readBuff[62] & 0x00FF);
    dataOut->fields.star3BZ = (readBuff[65] & 0x00FF) << 8 | (readBuff[64] & 0x00FF);
    dataOut->fields.star3OX = (readBuff[67] & 0x00FF) << 8 | (readBuff[66] & 0x00FF);
    dataOut->fields.star3OY = (readBuff[69] & 0x00FF) << 8 | (readBuff[68] & 0x00FF);
    dataOut->fields.star3OZ = (readBuff[71] & 0x00FF) << 8 | (readBuff[70] & 0x00FF);

}

最后我打印,例如YWheelSpeed

adcsTM191_measurements(&temp);  
printf("structure y wheel speed is: %d \n", temp.fields.YWheelSpeed);

这个值应该打印一个负值,它会:

structure y wheel speed is: -97

现在这是事情,如果我 print (readBuff[27] & 0x00FF) << 8 | (readBuff[26] & 0x00FF),它对应于 Y 轮速变量中填充的内容,它内部的任何地方adcsTM191_measurements(adcs_measurements_t* dataOut)都不会打印这个负值。而是打印无符号字符的最大值(65,535)。

int y = (int) (readBuff[33] & 0x00FF) << 8 | (readBuff[32] & 0x00FF);
printf("inside struct y is: %d", y);

我期望存储在结构内部会进行一种隐式转换,因此它会按预期打印负值。它是如何做到的?如何在不使用结构的情况下打印正确的值?

标签: cstructcastingprintfshift

解决方案


根据 C 2018 脚注 128,使用 , 定义的位字段是有符号还是无符号是实现定义intint YWheelSpeed。由于您的实现显示它的负值,推测它是有符号的,因此,作为一个 16 位有符号整数,它可以表示从 -32,768 到 32,767 的值。

我们还可以推断出,int在您的实现中超过 16 位,可能是 32 位(根据“65535”在int y打印“%d”的一种情况下打印的事实)。

考虑这个任务:

dataOut->fields.YWheelSpeed = (readBuff[33] & 0x00FF) << 8 | (readBuff[32] & 0x00FF);`

在此表达式中,readBuff[33]和由通常的促销readBuff[32]转换为。也是一个。int0x00FFint

如果我们假设readBuff[33]是 255 和readBufff[32]159(即 2 8 -97),那么右侧表达式的=值为 65,439(即 2 16 -97)。在赋值中,右操作数被转换为左操作数的类型,即 16 位有符号整数。在这种情况下,值 65,439 不能用 16 位有符号整数表示。C 2018 6.3.1.3 3 告诉我们“结果是实现定义的,或者引发了实现定义的信号。”</p>

这种转换的一个常见实现是产生模 2 16的结果,或者等效地,将 16 的低 16 位重新解释为int二进制补码 16 位整数。这产生 -97。由于您的实现随后显示 -97 的值,大概这就是您的实现所做的。

因此,dataOut->fields.YWheelSpeed赋值为 -97。稍后打印时:

printf("structure y wheel speed is: %d \n", temp.fields.YWheelSpeed);

然后默认参数 Promotions,包括通常的整数提升,temp.fields.YWheelSpeed从值为 -97 的有符号 16 位整数转换为值为 -97 的整数int,并打印“-97”。

相反,假设(readBuff[33] & 0x00FF) << 8 | (readBuff[32] & 0x00FF)打印为%d. 正如我们在上面看到的,这个表达式的值为 65,439,所以应该打印“65439”。

问题指出:

现在这是事情,如果我 print (readBuff[27] & 0x00FF) << 8 | (readBuff[26] & 0x00FF),它对应于 Y 轮速变量中填充的内容,......它会打印无符号字符的最大值(65,535)。

但是,(readBuff[27] & 0x00FF) << 8 | (readBuff[26] & 0x00FF)不是分配给 的值YWheelSpeed,它可能是“Y 轮速变量”。YWheelSpeed是从元素 32 和 33,而不是 26 和 27 分配的readBuff。因此,我们不应该对打印一些不同的值而不是 65,439 感到惊讶。


推荐阅读