Non-const pointer argument for double double pointer parameter

The modifier constin C ++ in front of the star means that with this pointer the specified value cannot be changed, and the pointer itself can point to something else. In the below

void justloadme(const int **ptr)
{
    *ptr = new int[5];
}

int main()
{
    int *ptr = NULL;
    justloadme(&ptr);
}

justloadme the function should not allow editing the integer values ​​(if any) indicated by the passed parameter, whereas it can edit the int * value (since const is not after the first star), but why am I getting a compiler error in both GCC and VC ++ ?

GCC error :: invalid conversion from int**toconst int**

Error VC ++: C2664: "justloadme": it is not possible to convert parameter 1 from "int **" to "const int **". Conversion loses qualifiers

, ? const? , strlen(const char*), const const char*

+5
1

, , . , , const-correctness :

const int constant = 10;
int *modifier = 0;
const int ** const_breaker = &modifier; // [*] this is equivalent to your code

*const_breaker = & constant;   // no problem, const_breaker points to
                               // pointer to a constant integer, but...
                               // we are actually doing: modifer = &constant!!!
*modifier = 5;                 // ouch!! we are modifying a constant!!!

, [*], . , :

int * const * correct = &modifier; // ok, this does not break correctness of the code
+8

All Articles