gpt4 book ai didi

一行投两次

转载 作者:太空狗 更新时间:2023-10-29 15:08:09 25 4
gpt4 key购买 nike

在一个项目中我看到了下面这行,我不明白为什么要投两次。

*((MY_STRUCT_T*)(void *)cp->down.common) = *(cp->dpr);

两次类型转换的原因是什么?为什么不直接转换为 MY_STRUCT_T*?


添加更多细节:

typedef union download_s {
MY_STRUCT_1_T *a1;
MY_STRUCT_2_T *a2;
void *common;
} download_t;

typedef struct cp_s {
...
MY_STRUCT_T *dpr;
...
} cp_t;

最佳答案

如果 cp->down.commonvoid * 已经转换为后者(“内部”转换为 (void *) ) 是不必要的。

这样就可以了:

*((MY_STRUCT_T *) cp->down.common) = *(cp->dpr);

另一种情况是,如果 cp->down.common 被声明为 intptr_t,那么确实会通过 void *(如中所示OP) 是必要的,因为只有 void * 保证适合 intptr_t

C 标准说:

[intptr_t] designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer

关于一行投两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21677656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com