Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

volatile and const pointer

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.

like image 1000
Yousf Avatar asked Mar 27 '12 12:03

Yousf


People also ask

What is difference between volatile and constant?

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.

Can we use volatile and const together?

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).

What is a volatile pointer?

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.

Can a variable be both const and volatile in C example?

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.


1 Answers

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

like image 119
hmjd Avatar answered Oct 05 '22 12:10

hmjd