I would like to know if this is possible and where something like this can be applied. I got asked this question somewhere and my thinking is you would have a const for something whose value you know is not going to change or rather you would not want to change. By definition however, volatile variables can change at any time even by code outside the current scope. So it seems to me that both these two qualifiers contradict each other.
2 Answers
Yes, there are cases where it can make sense. In summary, a const volatile means that the code cannot change the value of the variable, but something outside of the program can. Some usecases for this include:
Read-only hardware registers
Read-only shared memory buffers where one CPU writes and another only reads
Here is a good article with much more detail: http://embeddedgurus.com/barr-code/2012/01/combining-cs-volatile-and-const-keywords/
Comments
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. But this can be changed by the processor or hardware based on the interrupt condition. So when in the program, we want to read the value of this Status Register, it should read the actual value with out any optimization. For this reason, the Status Registers can be declared as volatile too. Example:
uint32_t volatile * const Spi_status_reg = (uint32_t *) 0x000800F0;
The best way to read a declaration like this is from the name of the variable back to the left: here Spi_status_reg is a constant pointer to a volatile 32-bit unsigned integer.
constdoesn't mean something won't change.