如何在不使用'的情况下从dell笔记本电脑定义访问keyValue和alternateKeyValue。'或者' - >'运算符直接引用qwerty结构或其成员。
我尝试寻找解决方案,但没有找到任何解决方案。你能帮我找个办法吗?
typedef enum
{
MOUSE_NONE,
MOUSE_UP,
MOUSE_DOWN,
MOUSE_LEFT,
MOUSE_RIGHT,
} mouse_direction_E;
typedef struct
{
bool leftButton;
bool rightButton;
bool middleButton;
bool mouseOn;
mouse_direction_E direction;
} mouse_S;
typedef struct
{
char keyValue;
char alternateKeyValue;
} keyboard_S;
typedef struct
{
mouse_S simpleMouse;
keyboard_S qwerty;
} laptop_S;
laptop_S dell =
{
.simpleMouse =
{
.leftButton = false,
.rightButton = false,
.middleButton = false,
.mouseOn = false,
.direction = MOUSE_NONE,
},
.qwerty =
{
.keyValue = '5',
.alternateKeyValue = '%'
},
};
答案 0 :(得分:1)
这听起来像是一个家庭作业问题......
如何:
int index = 0;
bool leftMouseButton = *( (bool *) &( ( (char*) &dell )[0]) );
/* \ 1 /
* \ 2 /
* \3/
* \___________ 4 _________/
* \_ 5 _/
* \_________________ 6 _________________/
*/
bool rightMouseButton = *( (bool *) &( ( (char*) &dell )[ sizeof(bool) - 1 ]) );
/* etc. Enjoy the math! */
用文字说明:
对于位于x
dell
字节的任何结构元素
dell
int
的大小相同,则将地址强制转换为int*
(请记住相应调整后续数学!)dell
开始的偏移量。告诉我如何完成作业!