- In parameter list
size_t strlen(const char *s);
The programmer of strlen() promises you that he/she will not alter the content pointed by s. It is good for you: You do not have to look up the manual for that bit of information. It tells more than you may think for the first time. It is good for the implementor of strlen(). He cannot accidentally write to the area pointed by s.
- Storing partial results as read-only variables.
const double aplusb = a + b; const double amulb = a * b; return 2*amulb/aplusb; // harmonic mean
It helps avoiding accidents and ease interpretation by humans.
- Pointing to literals
const char *appname = "Hello World!";
If you turn on the displaying of warnings in your compiler then you will see that pointing to a literal with non-const pointer is discouraged. It also helps not making bugs.
2010-04-26
Yes, it is C. The "const" keyword
The const qualifier can have multiple useful meanings depending on the context.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment