I have a come across following definition in an embedded code:
#define REG_ADD 0x20081004
#define pREG ((void * volatile *)REG_ADD)
Why there are 2 *
in pREG
definition? What does it mean?
I have a come across following definition in an embedded code:
#define REG_ADD 0x20081004
#define pREG ((void * volatile *)REG_ADD)
Why there are 2 *
in pREG
definition? What does it mean?
void**
is a pointer to pointer-to-void.
void * volatile *
is a pointer to volatile-qualified-pointer-to-void. (Read the declaration right-to-left, pretty much. See this.)
What this means in plain English is that pREG
is likely a pointer to some sort of hardware index register, which in turn contains an address. To tell the compiler that this index register can get updated at any moment, the register itself should be treated as volatile
.
A somewhat more readable way to write the same would be:
typedef void* volatile reg_add_t;
reg_add_t* pREG = (reg_add_t)0x20081004u;
Please note that the use of void*
for this purpose is questionable. This register will have a defined use, possibly it should have been a uint32_t * volatile
or uint8_t * volatile
instead.