1

i'd like to know if there is a sort of implicit conversion between variables when using a pointer to constant integer , for example , if i use an address of a variable type int or const int it accepts to store it , however if i use a normal pointer to int it doesn't allow storing the address of the const int type,why is this?, thanks in advance

int i=4;
const int ii=4;

//pointer to constant int 
const int *pci=&i; //OK.
pci=ⅈ //OK.
int *pi=ⅈ //ERROR invalid conversion.
Tamir Vered
  • 10,187
  • 5
  • 45
  • 57
  • 3
    Are you asking for `c++` or `c`? Different languages; Different standards; Different meaning for `const`. – StoryTeller - Unslander Monica Nov 27 '16 at 13:45
  • 3
    `int*` could implicitly convert `const int*`, conversely no. In C++. – songyuanyao Nov 27 '16 at 13:46
  • Assigning a const pointer to a non-const pointer is wrong. The original was marked as const for a reason, therefore it's considered wrong to just assign it to a non-const pointer. However I think you can achieve what you want with const_cast – notadam Nov 27 '16 at 13:49

2 Answers2

1

The first and second assignments initialize pci to constly point to an int or a const int.

So you might have one of two situations:

  • const int* which points to an int.

    pci=&i;
    
  • const int* which points to a const int.

    pci=ⅈ
    

Both cases are safe because you are only adding a constraint.

By doing:

int *pi=ⅈ

You make an int* point to a const int which means you remove a constraint.

Since removing a constraint might be risky, this requires you to use a const_cast.

int* pi = const_cast<int*>(&ii);

Note that forcibly removing the const modifier is something you should ask yourself twice if you really wanna do, since it also make the const modifier somewhat meaningless because you will be able to modify that "constant" address through the converted variable.

Community
  • 1
  • 1
Tamir Vered
  • 10,187
  • 5
  • 45
  • 57
  • 1
    @StoryTeller I'm still confused with that, after all these decades. That may be a reason why I'm avoiding pointers like hell in c++. – πάντα ῥεῖ Nov 27 '16 at 14:01
  • 1
    @πάνταῥεῖ, it gets simpler after you down a couple of pints. – StoryTeller - Unslander Monica Nov 27 '16 at 14:02
  • 1
    @StoryTeller I'm on it, THX for the hint ;) – πάντα ῥεῖ Nov 27 '16 at 14:04
  • It makes sense. `const` means that "i don't want this to change", right? So it makes sense to prevent it from being assigned to a pointer through which it can be modified. That would defeat the purpose of `const`. However, there are special cases where you may actually want this, hence `const_cast`. So you are allowed to do the conversion after all, but you need to explicitly state it in your code, making it clear what you're doing, which makes your (or your team's) life easier when trying to debug things. It clearly states what's happening, instead of potentially braking things silently. – notadam Nov 27 '16 at 14:05
  • @adam10603 There was some saying to read from inner to outer asterisks in a spiral or so ... – πάντα ῥεῖ Nov 27 '16 at 14:07
1

Simply assigning a pointer to a const element to a pointer to a non-const element is not allowed, since that would silently dismiss the const-ness of the original, which is something that you don't want, and can also be a source of silent bugs, which is one of the reasons it's not allowed.

However, if this is really what you want, you can explicitly request to remove the const qualifier by using const_cast

notadam
  • 2,754
  • 2
  • 19
  • 35