【发布时间】:2021-07-26 19:30:01
【问题描述】:
我有以下typedefined struct:
typedef struct
{
uint8_t u8Byte1; // This byte takes the needed values sometimes
uint8_t u8Byte2; // Not used
uint8_t u8Byte3; // This byte takes the needed values the other times
uint8_t u8Byte4; // Not used
} tstrMapMetadata;
我有一个线程填充(来自传感器的数据)这个结构并使用它的一个值:
while(true)
{
tstrMapMetadata * pstrMapMetadata = something();
if(pstrMapMetadata->u8Byte1 == SOMETHING) //<---- this line
{
//Do something special
}
}
但现在我有一个condition(在线程期间是常量),我希望用u8Byte3而不是u8Byte1比较标记的行。
当然可以
if(((condition) ? pstrMapMetadata->u8Byte1 : pstrMapMetadata->u8Byte3) == SOMETHING) //<---- this line
但我会一直做相同的比较。
有没有办法在声明之前指向结构的成员之一(它们是这样称呼的吗?)?
类似的东西(这个代码当然不起作用,但可以让我知道我在寻找什么):
uint8_t * coolPointer;
if(condition)
{
coolPointer = (someOperation)tstrMapMetadata(u8Byte1);
}
else
{
coolPointer = (someOperation)tstrMapMetadata(u8Byte3);
}
while(true)
{
tstrMapMetadata * pstrMapMetadata = something();
if(coolPointer == SOMETHING) //<---- this line
{
//Do something special
}
}
谢谢!
【问题讨论】:
-
第一种方法有什么问题?
-
@WaisKamal 它进行的比较将始终具有 same 结果,我希望在
while循环开始之前只进行一次比较跨度> -
你的意思是
u8Byte1和u8Byte3总是有相同的值吗? -
@WaisKamal 不,我的意思是循环内部
case不会改变 -
@Jarod42 当然!抱歉,我正在编辑它
标签: c++ pointers struct typedef