const T (*ptr);
T const (*ptr);
T (*const ptr);
const T * const ptr;
T const * const ptr;
Maybe typedef will help you
1 2 3 4 5 6 7
typedefint * ptr_int;
typedefconstint * ptr_const_int;
ptr_int p; //a pointer to int
const ptr_int p; //a constant pointer to int
ptr_const_int p; //a pointer to constant int
const ptr_const_int p; //a constant pointer to constant int
I feel that it is better to write only intconst good; because it is similar to the ordering of void * const good;.
However I think const is not very useful, because a function that takes a pointer to a constant, int atoi(charconst*c);, is not guaranteed to adhere to it. It might cast the const away. So the compiler cannot use const to make any optimizations.
because a function that takes a pointer to a constant, int atoi(char const*c);, is not guaranteed to adhere to it. It might cast the const away. So the compiler cannot use const to make any optimizations.
The compiler can and does use const to make optimizations. In such instances where the const is cast away, you might get results you don't expect. Which is exactly why you shouldn't cast away const qualifiers.