简体   繁体   English

取消引用的联合成员字节不相同

[英]Dereferenced union member bytes are not the same

From the tutorial at learnc, I am experimenting with some really basic stuff on pointers and unions. 从learnc的教程中,我正在尝试一些关于指针和联合的非常基础的东西。 In the code below I create a struct operator with an anonymous union consisting of a float , double , and int . 在下面的代码中,我创建了一个带有匿名unionstruct operator ,该union包括floatdoubleint Since double is the biggest one at eight bytes, I expect to see my int have eight bytes, which it does. 由于double是八个字节中最大的一个,因此我希望我的int具有八个字节,它确实如此。 However, they are not the same bytes as the double! 但是,它们与double字节不同!

typedef enum {
    INTEGER = 0,
    FLOAT = 1,
    DOUBLE = 2,
} operator_type;

typedef struct operator {
    operator_type type;

    union {
        int intNum;
        double doubleNum;
        float floatNum;
    };
} operator_t;

int main() {

    operator_t op;
    op.type = FLOAT;
    op.floatNum = 3.14f;

    printf("op.intNum = %i\nop.doubleNum = %f\nop.floatNum = %f\n", op.intNum, op.doubleNum, op.floatNum);

    printf("op.intNum [%i, %i, %i, %i, %i, %i, %i, %i, %i]",
           &(op.intNum) + 0,
           &(op.intNum) + 1,
           &(op.intNum) + 2,
           &(op.intNum) + 3,
           &(op.intNum) + 4,
           &(op.intNum) + 5,
           &(op.intNum) + 6,
           &(op.intNum) + 7,
           &(op.intNum) + 8
           );

    printf("op.doubleNum [%i, %i, %i, %i, %i, %i, %i, %i, %i]",
           &(op.doubleNum) + 0,
           &(op.doubleNum) + 1,
           &(op.doubleNum) + 2,
           &(op.doubleNum) + 3,
           &(op.doubleNum) + 4,
           &(op.doubleNum) + 5,
           &(op.doubleNum) + 6,
           &(op.doubleNum) + 7,
           &(op.doubleNum) + 8
    );

    return 0;
}

I get the output: 我得到的输出:

op.intNum [-13304, -13300, -13296, -13292, -13288, -13284, -13280, -13276, -13272]
op.doubleNum [-13304, -13296, -13288, -13280, -13272, -13264, -13256, -13248, -13240]

Shouldn't &(op.intNum) == &(op.doubleNum) == &(op.floatNum) ? &(op.intNum) == &(op.doubleNum) == &(op.floatNum)吗?

Shouldn't &(op.intNum) == &(op.doubleNum) == &(op.floatNum) ? &(op.intNum) == &(op.doubleNum) == &(op.floatNum)吗?

Yes, they should, and they are. 是的,他们应该,而且确实如此。

In order to print an address, use %p as the format specifier, and cast it to void* . 为了打印地址,请使用%p作为格式说明符,并将其强制转换为void* Read more in How to print variable addresses in C? 如何在C中打印变量地址中阅读更多内容

Edit: Why do I have to cast my memory address to (void *)? 编辑: 为什么我必须将内存地址转换为(void *)?

&(op.intNum) + 1 is the address immediately after the end of op.intNum . &(op.intNum) + 1是紧接在op.intNum之后的地址。

That is, if op.intNum is at address A, and sizeof op.intNum is 4, then the expression you wrote has value A+4. 也就是说,如果op.intNum位于地址A,而opof.intNum的大小为4,则您编写的表达式的值为A + 4。

That's a consequence of how pointer arithmetic is defined. 这是如何定义指针算法的结果。

In your case you don't dereference union, for dereferencing you need to declare op as pointer. 在您的情况下,您不取消引用联合,要取消引用,则需要声明op作为指针。

FYI: 供参考:

In your code you print 在您的代码中打印

  • address of int member, and sizeof(int) is 4 in your architecture, so increment by 1 , your address will be address + sizeof(int) int成员的地址,在您的体系结构中sizeof(int)为4,因此加1,您的地址将为address + sizeof(int)

  • address of double member and sizeof(double) in 8, in this case each after each increment your address will be address + 8 double成员的地址和sizeof(double)的8,在这种情况下,每次递增后,您的地址将为address + 8

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM