Pointer declared as constant as well as volatile

前端 未结 6 1022
北海茫月
北海茫月 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:47

    *const volatile char *p=(const volatile char ) 0x30;
    What is meant by: The value of p is changed by external conditions only.

    Conceptually, you can think of this type of variable as a logical viewer. Similar in concept to the peephole in a door. A peephole allows you to view what is on the other side of the door, but does not allow you to change what is on the other side (const). However, conditions on the outside of the door can change on their own volition (they are volatile). You can see what happens, but you cannot change what happens.

    In an embedded system, for example, there are hardware registers designed to provide state information about events happening in the outside world. An optical encoder, for example, used to sense RPM will set a value in a register. Each rotation, it senses light from an led and modifys the value in a hardware register. This is what is meant by external conditions. On the other side of the picture, i.e. in your code (perhaps a PID control loop), you can read this information to use in providing adjustments to the loop, but you cannot change this value, nor would you want to. (const)

    From the perspective of your software then, this illustrates:

    enter image description here

提交回复
热议问题