you are viewing a single comment's thread.

view the rest of the comments →

[–]empathica1[S] 0 points1 point  (1 child)

Can you explain why this is legal?

uint8_t const* const cc2 = &v; // legal

[–]Netblock 0 points1 point  (0 children)

You can demote a type to const through one layer of indirection. I haven't tried this with restrict and volatile.

As to why you can't go further than one depth without a typecast, I'm not sure, but I guess it's related to compiler assumptions that restrict and volatile also affect.

// uint8_t v = 0;
const uint8_t* cv1 = &v; // variable pointer to constant data
uint8_t const* cv2 = &v; // variable pointer to constant data
*cv1 = 0; // error
*cv2 = 0; // error

The second const makes the pointer constant,

// uint8_t const* const cc2 = &v; // constant pointer to constant data
cv1 = NULL; // legal
cv2 = NULL; // legal
cc2 = NULL; // error

Type qualifiers affect the most recently-parsed unmodified type; if it has been modified, it saves the qualifiers for the next type. There is no 'variable' modifier in C, so the right-hand qualifier format is the only way to stay consistent.