Pointer declared as constant as well as volatile

前端 未结 6 1005
北海茫月
北海茫月 2021-01-31 07:04

While reading I came across this type of declaration and the following line -

const volatile char *p=(const volatile char *) 0x30;
6条回答
  •  無奈伤痛
    2021-01-31 07:44

    Consider a read-only hardware register, of your network card for example.

    It might change outside the control of the program, so the compiler is not allowed to cache its value in a register or optimize it away. Thus, volatile.

    And it's read-only, so you shouldn't write to it. Thus, const.

提交回复
热议问题