5

Consider the following case 1:

const int n = 5;
int* p = &n; 

This is invalid, because &n is of type cont int* and p is of type int * (type mismatch error).

Now, consider this case 2:

int k = 4;
int *const p = &k; 

This case compiles successfully, without any error. Clearly, p is of type int * const and &k is of type int *. In this case, there is a type mismatch, but it is valid.

Question : Why is the second case valid, even though there is a type mismatch?

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
  • You can assign a non-const to a const, but not vice versa. – wcochran Jan 14 '21 at 06:25
  • 1
    It is always legal to "const"-ify access, but not the other way around. If you have read/write access, it's OK to not use the write part and stay read-only. But if you only have read rights, you can't claim write access. – dxiv Jan 14 '21 at 06:25
  • Those are two different consts. One is a low-level const and the other is a top-level const. – MahanGM Jan 14 '21 at 06:33
  • 1
    1. Pointer (or reference) to `const` is only a promise not to modify the object via that pointer/reference. It does not mean that the object is unmodifiable. 2. `int *const` is a constant pointer (the pointer cannot be reassigned to point to something else), not a pointer to `const`. – jamesdlin Jan 14 '21 at 06:33
  • So you would think that`const int n = 5; int *const p = &n;` is legal? No. It does matter where const is. Constant pointer to int. Of course you can initialize a constant with a gvalue – Swift - Friday Pie Jan 14 '21 at 06:41

2 Answers2

4

In this case, there is a type mismatch

No; there is no type mismatch in this case. It is a pointer to non-cost and you initialise it with a pointer to non-const.

Alternatively, if you insist on there being a "mismatch", then it is analogous to the following "mismatch":

const int b = 42;

Why is the second case valid

Simply put: The constness of the initialiser is irrelevant to whether it initialises a const object or not. Besides, the initialiser is a prvalue of a non-class type so const qualification doesn't even apply to it.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    I mentioned this on another answer, but wouldn't it be preferable to close this question as a duplicate instead of answering it? – cigien Jan 14 '21 at 06:26
  • 7
    @cigien If you've found a duplicate, feel free to vote to close. – eerorika Jan 14 '21 at 06:27
  • 1
    Sorry, let me clarify. Given your knowledge of the content on SO in the c++ tag, I'm sure you could find a duplicate in about the same time as it would take to write an answer, and with your c++ hammer you could close the question yourself. Wouldn't that be preferable to having a lot of identical questions around? – cigien Jan 14 '21 at 06:29
  • @cigien, what prevent you from doing it ? – Nicolas Dusart Jan 14 '21 at 07:35
  • @NicolasDusart There's nothing preventing me. I could even close this as a duplicate of a target answered by eeorika, as they have answered essentially this question multiple times before. It's true that finding targets can be harder than simply answering a simple question, but my hope was that I could convince a high rep user of the benefit of taking the effort to find a target instead of repeatedly answering basic questions like this. Still, if the priorities of some users are different, there's no rule preventing from behaving in in this fashion. – cigien Jan 14 '21 at 08:26
1

Firstly, int *const does mean a const pointer to a non-const int. So there is absolutely no type mismatching between pointer and pointee types.

Secondly, you can always take the address of a non-const variable into a pointer to a const. So this would be valid too:

int n = 5;
const int * p = &n;
Nicolas Dusart
  • 1,867
  • 18
  • 26