I'm trying to understand the correct use of the -> operator in C. I think I have the understanding, but need to have that validated. so...
if i have an 32 bit data structure (eg. MyStruct) to store some status data (1s or 0s) which is pointed to by pnt. there are 3 members of that data structure ('first', 'second' both 8 bit, and 'third' 16-bit)
If I have
pnt -> second ^= (1<<3)
this can be written as
pnt -> second = pnt -> second ^ (1<<2)
then this is saying that; get the value of 'second' from the structure MyStruct that is being pointed to by pnt. XOR that value with a bit that has been shifted left (i.e. 00000100). Whatever the result, put that back into MyStruct at the member 'second'
yes?
Thanks.
CodePudding user response:
Yes, the compound assignment
A ^= B;
has the same effect as
A = A ^ B;
Note that pnt -> second ^= (1<<3)
can not be written pnt -> second = pnt -> second ^ (1<<2)
though. 1<<3
is not the same as 1<<2
.
CodePudding user response:
pnt->second
is a shorthand for (*pnt).second
which means "take the struct pointed to by pnt
and from that struct, get the element second
".
CodePudding user response:
As for the assignment part of your question, the statements A=A XOR B is identical to A XOR= B, as with many other operators.
just make sure to change the (1<<2)(1<<3) difference between the lines.
As for the header of your question regarding the arrow(->) symbol: Given a struct A, you can reference a field (second) within the struct by two ways -
- A.second
- (&A)->second - that is, if you have a pointer to A, call it B (*B=A) - so referencing will be done thus: B->second.
hope that clarifies your question.