Consider the following code:
void Increment(int *arr) {
arr++;
}
int main() {
int arr[] = {1,2,3,4,5};
// arr++ // illegal because its a const pointer
Increment(arr); // legal
}
My question is if arr is a const pointer, how come I can send it to a function that doesn’t receive a const pointer?
The code compiles without the warning of discarding const qualifiers.
Don’t get fooled by the pointer. The same holds for plain ints:
In summary,
constapplies to each object individually. A copy of a const object doesn’t need to be const too.