#include <stdio.h>
#include <stdint.h>
struct s1
{
uint8_t a;
uint16_t b;
uint8_t c;
};
struct s2
{
uint8_t d;
uint8_t e;
};
int main()
{
struct s1 *d1;
struct s2 *d2;
d1->a = 1;
d1->b = 2;
d1->c = 3;
d2->d =4;
d2->e =5;
&d1->b = d2;
return 0;
}
為什么不起作用&d1->b = d2;?b是 32 位struct s2還是 32 位?因為我們只是分配地址。
如果該行更改為d1 = (void *)d2;它可以正常作業。
uj5u.com熱心網友回復:
第一的。指標d1和d2未初始化。使用此指標會呼叫未定義行為。為了修復它,我建議使用自動存盤制作實際物件。
struct s1 d1;
struct s2 d2;
您不能分配給d2,d1.b因為型別不匹配。struct s2并且uint16_t不兼容。要執行這種型別的雙關語,請使用memcpy().
memcpy(&d1.b, &d2, sizeof d2);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322567.html
