There are many other subtle points surrounding const correctness in C++. I suppose the question here has simply been about C, but I'll give some related examples since the tag is C++ :
- You often pass large arguments like strings as `TYPE const &` which prevents the object from being either modified or copied. Example :
`TYPE& TYPE::operator=(const TYPE &rhs) { ... return *this; }`
But `TYPE & const` is meaningless because references are always const.
- You should always label class methods that do not modify the class as `const`, otherwise you cannot call the method from a `TYPE const &` reference. Example :
`bool TYPE::operator==(const TYPE &rhs) const { ... }`
- There are common situations where both the return value and the method should be const. Example :
`const TYPE TYPE::operator+(const TYPE &rhs) const { ... }`
In fact, const methods must not return internal class data as a reference-to-non-const.
- As a result, one must often create both a const and a non-const method using const overloading. For example, if you define `T const& operator[] (unsigned i) const;`, then you'll probably also want the non-const version given by :
`inline T& operator[] (unsigned i) {
return const_cast<char&>(
static_cast<const TYPE&>(*this)[](i)
);
}`
Afaik, there are no const functions in C, non-member functions cannot themselves be const in C++, const methods might have side effects, and the compiler cannot use const functions to avoid duplicate function calls. In fact, even a simple `int const &` reference might witness the value to which it refers be changed elsewhere.