I saw this code snippet
const volatile int * volatile * const X;
but I can't understand what does the second * means.
I understand that
const volatile int * volatile const X;
means a volatile const integer pointer to a volatile const data.
The const keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter. The volatile keyword specifies that the value associated with the name that follows can be modified by actions other than those in the user application.
Yes a C++ variable be both const and volatile. It is used in situations like a read-only hardware register, or an output of another thread. Volatile means it may be changed by something external to the current thread and Const means that you do not write to it (in that program that is using the const declaration).
A pointer of the form. volatile int* p; is a pointer to an int that the compiler will treat as volatile . This means that the compiler will assume that it is possible for the variable that p is pointing at to have changed even if there is nothing in the source code to suggest that this might occur.
Yes, it is possible. The best example is Status Register in controllers, in the program we should not modify this Status Register so it should be a constant.
A useful site for understanding non-trivial C declarations is cdecl.org.
The description for:
const volatile int * volatile * const X;
from cdecl is:
declare X as const pointer to volatile pointer to const volatile int
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With