What are the differences between const and volatile pointer in C?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The difference really comes down to the difference between
constandvolatile. The only things these two concepts have in common is syntax.constis compiler-enforced and says “the programmer can not change this.”volatilesays “this data might be changed by someone else” and so the compiler will not make any assumptions about that data. Withoutvolatilethe compiler might say “I put this data from memory into a register, and since I haven’t done anything to that data, I’m sure it’s the same and I don’t need to read it into the register again.” When the data is marked asvolatilethe compiler won’t make such an assumption (because someone else might have changed the data) and so it will reread the data into the register.Now, are you asking for the difference between
and
or the difference between
and
In the former case:
pis a pointer to anintand where that pointer points can not be changed by the programmer whereasqis a pointer to anintand where that pointer points could be changed by someone other than the programmer so the compiler makes no assumptions about that pointer.So:
In the latter case:
pis a pointer to anintand whatpis pointing to can not be changed by the programmer whereasqis a pointer to anintand whatqis pointing to could be changed by someone other than the programmer so the compiler makes no assumptions about that data.