1

The following code fails to compile with two errors on the line 'func(&pobj);' stating "Cannot convert 'X * *' to 'const X * *' in function main()" and "Type mismatch in parameter 1 (wanted 'const X * *', got 'X * *') in function main()" even though its the correct way to pass pointer parameters. And also on commenting this particular line i.e 'func(&pobj);' and uncommenting the two lines above it which declares a new 'const' pointer 'pp' of required type (in parameter of func) but still throws an error on the line which declares and assigns 'pp' saying "Cannot convert 'X * *' to 'const X * *' in function main()".

using X = int;

void func(const X **);

int main() {
    X *pobj = new X(58);

    // const X **pp = &pobj;  // ERROR
    // func(pp);

    func(&pobj); // ERROR
}

I believe that this is the correct way to pass constant pointers as parameters and I just don't understand why the program fails to compile. Can anyone point out the fault in the code above and suggest me the correct logic/syntax if I am wrong? Thanks in advance.

UPDATE: This question was marked as duplicate but it doesn't have the answer that solves the problem at hand. Thus I would love it if the Stack Overflow community would help me to solve my problem here. Thanks.

hecate
  • 620
  • 1
  • 8
  • 33

3 Answers3

2

Do this:

const X * pobj = new X(58);
// ^^^^^^

func(&pobj);

Now pobj is a pointer-to-const-X, and its address is of type const X ** as required.

If you want to retain the original (mutable) pointer, you need to make a new const pointer first:

X * p = new X(58);
const X * q = p;

func(&q);
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • That solves the problem for the module call but this doesn't allow us to change the value pointed by pointer 'pobj' which is essential for my program (this is only a small part of it which has the error). Do you have any idea to solve this issue/error ? Thanks for reply. – hecate Jun 08 '16 at 12:42
  • @hecate: Sounds like you didn't define your module API correctly then. If you want to modify a thing, you need to take a pointer to a modifyable thing. Maybe check the other answer. – Kerrek SB Jun 08 '16 at 12:46
  • Note that the second solution you gave to retain in original pointer doesn't work.. :-( – hecate Jun 08 '16 at 12:52
  • @hecate: Really? [Works perfectly](http://melpon.org/wandbox/permlink/AZsyjab3W73fcvXu) for me... – Kerrek SB Jun 08 '16 at 13:09
2

You will need to apply const to pointer-to-pointer:

void func(X *const *);

...
     X * const *pp=&pobj; // ERROR    
     func(&pobj); // ERROR
...

void func(X * const * obj) { // module code goes here
}
Ajay
  • 18,086
  • 12
  • 59
  • 105
  • Heyy thanks man that really solves the problem. But would you mind explaining me the theory/meaning/logic of putting 'const' between the two asterisks (*) as it is not the usual way to do it? And can you also tell me why the program failed when the function is called using just 'func(&pobj);' without declaring the pointer 'pp' ? Thanks again pal. :-) – hecate Jun 08 '16 at 12:49
  • In short: `const X*` is actually `X const*` - making the pointer type to be `const` (and not `X`). `const X**` is `X const* *`, and not `X * const*`. . `X* const *` can point to `const X*`, and you can make `X* obj` to be `X const* obj` to make this possible. See the "duplicate" link to find more about it – Ajay Jun 08 '16 at 13:05
0

If the answer of Ajay is not appropriate, because You cannot change the interface of func(),

and if the answer of Kerrek does not work, because func() changes the pointer, which must be assigned back, that does not happen:

X * p = new X(58);
const X * q = p;
func(&q);
p = q;   // errror

then simply use a cast:

func(const_cast<const X**>(&p))
Roland
  • 336
  • 2
  • 8