Reputation: 1455
I have the following typedef
ined 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;
And I have a thread that fills (with data from a sensor) this struct and uses one of its values:
while(true)
{
tstrMapMetadata * pstrMapMetadata = something();
if(pstrMapMetadata->u8Byte1 == SOMETHING) //<---- this line
{
//Do something special
}
}
But now I have a condition
(constant during the thread) in which I want the comparison of the marked line done with u8Byte3
instead of u8Byte1
.
Of course I could
if(((condition) ? pstrMapMetadata->u8Byte1 : pstrMapMetadata->u8Byte3) == SOMETHING) //<---- this line
But the I would be doing the same comparison all the time.
Is there a way to point to one of the members (are they called like this?) of the struct before its declaration?
Something like (this code of course doesn't work but gives an idea of what I'm looking for):
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
}
}
Thanks!
Upvotes: 2
Views: 135
Reputation: 217265
There is pointer to member:
uint8_t tstrMapMetadata::*member = condition ?
&tstrMapMetadata::u8Byte1 :
&tstrMapMetadata::u8Byte3;
while (true)
{
tstrMapMetadata* pstrMapMetadata = something();
if (pstrMapMetadata->*member == SOMETHING) // usage with ->* ( .* for objet )
{
//Do something special
}
}
Upvotes: 7