Consider the if statement:
if (((DbSignal*) ev->newVal.buff)->sig)
Where DbSignal
is a structure.
Why is DbSignal within brackets and what is the asterisk operator doing in this case?
The syntax (DbSignal*)
is a typecast. It converts one type to another.
In this case, the operand of the cast is ev->newVal.buff
which presumably is a pointer to a character buffer. This pointer is converted to a pointer to DbSignal
via the cast. The result is then dereferenced and the sig
member is accessed.
It is the cast: ev->newVal.buff
is casted to pointer to DbSignal
. Then this pointer is being dereferenced (sig
member accessed)
What is the type cast: What exactly is a type cast in C/C++?
We have ev
which is a pointer in this case.
it points to a struct containing the variable newVal
which contains a buff
pointer.
So we have ev->newVal.buff
Here buff is either a char*
or void*
(a series of bytes, but apparently has some layout). Meaning that the memory it points to could potentially be interpreted in different ways.
By your example, we know that buff
has a certain layout, corresponding to the DbSignal
struct.
So in order to access ->sig
we have to cast this .buff
to DbSignal
, basically telling that we want to interpret that memory region with the layout described by DbSignal
.
Hope this gives some context.