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?

5
  • You can assign a non-const to a const, but not vice versa. Commented Jan 14, 2021 at 6: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. Commented Jan 14, 2021 at 6:25
  • Those are two different consts. One is a low-level const and the other is a top-level const. Commented Jan 14, 2021 at 6: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. Commented Jan 14, 2021 at 6:33
  • So you would think thatconst 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 Commented Jan 14, 2021 at 6:41

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I mentioned this on another answer, but wouldn't it be preferable to close this question as a duplicate instead of answering it?
@cigien If you've found a duplicate, feel free to vote to close.
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, what prevent you from doing it ?
@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.
2

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; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.