Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't it legal to convert "pointer to pointer to non-const" to a "pointer to pointer to const"

It is legal to convert a pointer-to-non-const to a pointer-to-const.

Then why isn't it legal to convert a pointer to pointer to non-const to a pointer to pointer to const?

E.g., why is the following code illegal:

char *s1 = 0; const char *s2 = s1; // OK... char *a[MAX]; // aka char ** const char **ps = a; // error! 
like image 529
ashishsony Avatar asked Feb 08 '10 10:02

ashishsony


People also ask

Is a non const pointer that points to a constant value?

A pointer to a const value (sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value. In the above example, ptr points to a const int . Because the data type being pointed to is const, the value being pointed to can't be changed. We can also make a pointer itself constant.

Can be marked as a pointer to const?

The pointer argument '%argument%' for function '%function%' can be marked as a pointer to const (con. 3). A function with a T* argument has the potential to modify the value of the object. If that is not the intent of the function, it is better to make the pointer a const T* instead.


1 Answers

From the standard:

const char c = 'c'; char* pc; const char** pcc = &pc;   // not allowed *pcc = &c; *pc = 'C';                // would allow to modify a const object 
like image 184
AProgrammer Avatar answered Oct 09 '22 05:10

AProgrammer