0

Assume

int a = 1;
int b = 2;
int c = 3;

int * A[3] = {&a, &b, &c};

I wonder why this is allowed:

void arrFunc1(int * const * arg)
{
}

And this one isn't:

void arrFunc2(int const ** arg)
{
}

In a:

int main(void) 
{
    arrFunc1(A);    // OK
    arrFunc2(A);    // Not OK

    return 0;
}

I know the difference between these two. First function will assume that the pointers themselves are constants and cannot be changed, and the second one assumes that the integers, to which these pointers point cannot be changed. But I wonder why the second function is not allowed? I can't see any reason of how this could break anything.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Ok, I have found the answer in the proposed 'duplicate question'. It is explained [here](http://c-faq.com/ansi/constmismatch.html) Thanks to everybody – Andrejs Gasilovs Jun 29 '17 at 09:50
  • Basically, you can convert (assign) `T` to `const T`, and `T *` to `const T *`, but it stops here, one can not go further and do `T **` to `const T **`, the reason is explained in the link above. – Andrejs Gasilovs Jun 29 '17 at 10:01

1 Answers1

0

Because with int const ** arg you say that arg is a pointer to a pointer to a const int, but a, b and c are not const int variables.

With int * const * arg you say that arg is a pointer to a constant pointer to an int. I.e. that you can not change what e.g. arg[0] is pointing to.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Yes, `a`, `b`, and `c` are not `const int`, but this doesn't stop me from calling a function that will assume that they are const, say `void func(const int a)`. In my case I'm adding a layer of indirection but what I'm asking is still exactly the same, I can't see how it could break anything – Andrejs Gasilovs Jun 29 '17 at 09:41