Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's meaning of these volatile with pointers in C ?

volatile void * ptr;

Whether ptr is volatile or it points to the volatile location.

So the actual doubt is : Is the same thing applied to the above declaration as it applied with const qualifier ?

Little explanation will help me a lot.

like image 414
Omkant Avatar asked Nov 06 '12 11:11

Omkant


People also ask

What is volatile pointer in C?

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 have volatile pointer?

Yes, a pointer can be volatile if the variable that it points to can change unexpectedly even though how this might happen is not evident from the code. An example is an object that can be modified by something that is external to the controlling thread and that the compiler should not optimize.

What is volatile and non volatile in C?

The volatile memory stores data and computer programs that the CPU may need in real-time, and it erases them once a user switches off the computer. Cache memory and RAM are types of Volatile memory. Non-volatile memory, on the other hand, is static. It remains in a computer even after a user switches it off.

What is volatile datatype?

volatile is a keyword known as a variable qualifier, it is usually used before the datatype of a variable, to modify the way in which the compiler and subsequent program treat the variable. Declaring a variable volatile is a directive to the compiler.


1 Answers

It's a pointer to volatile data. If the pointer itself should be volatile but not the data it points at, you'd use:

void * volatile ptr;

So yes, it works the same way as the const modifier.

like image 129
unwind Avatar answered Sep 19 '22 00:09

unwind