For a function definition which contains the declaration of type void foo(const int ) Both of the following declarations are valid.
void foo(const int); // valid
void foo(int); // valid, const can be omitted.
But if a function definition contains a declaration of type void foo(const int*) omitting const is ilegal:
void foo(const int *); // valid declaration
void foo(int *); // error: const cannot be omitted.
Why is const cannot be omitted in the function declaration if its parameter has pointer types? What makes the difference?
You can only omit the const specifier when it’s applied directly to the parameter. In the pointer case, it’s applied to the thing being pointed at, not the pointer itself, so there’s an extra level of indirection between const and the parameter. You could omit it in this case: