1

Ok, so im reading the C++ Primer, Fifth Editon and im learning about constexpr for the first time. It starts by saying:

It is important to understand that when we define a pointer in a constexpr declaration, the constexpr specifier applies to the pointer, not the type to which the pointer points:

 const int *p = nullptr; // p is a pointer to a const *q = nullptr; // q is a const pointer to int 

Ok so i think to myself... Well it p is a pointer to a const then it means that p(the pointer) it-self is not a constant so i may change it. So ofcourse, i tried it out on my IDE:

#include <iostream> #include <list> #include <vector> #include <string> int main() { const int x = 0; const int y = 30; const int *p = x; *p = &y; return 0; } 

Guess what. It gave me a error when i try to assign *p to the adress of constant y. Well the error specifically

error: assignment of read-only location '* p'| 

Wow i was suprised. I Really thought the book said p is a pointer to a const. SO i thought p is not a constant it self, so you can change it.? Or is my anaolgy wrong??

And then ofcourse it tells me:

constexpr int *q = nullptr; // q is a const pointer to int 

Well if my previous anaology was correct then, this pointer is an actual const it-self. So it may not be changed..? Or am i still wrong?

Constexpr

Ok guys so i understood. I shouldnt derefrence when i assign pointers to "objects" or anything. But now i get this error when i try out constexpr for the first time!

error: invalid conversion from 'const int*' to 'int*' [-fpermissive]| 

And this is my code:

int main() { const int a = 0; const int i = 5; constexpr int *w = &a; return 0; } 
4
  • 2
    Your error is actually because you need to assign the pointer to the address of a variable: const int *p = &x; And of course you cannot assign *p, because the resulting int is const! (Not to mention that you're attempting to assign an const int to a const int*) Commented Apr 28, 2016 at 12:03
  • Ok I just got that, and tried it quickly on my IDE. But same problem in my IDE. And im using codeblocks, so it tells me exactly where the error was. And ofcourse the error is pointing at where i declare *p = &x @AndyG Commented Apr 28, 2016 at 12:04
  • Read the edits to my comment. I address the other issues. Commented Apr 28, 2016 at 12:05
  • Ok Thanks a lot @AndyG ! I just messud up, when i tried to assign the pointer. Turns out i dont need the astrix.... -.- Commented Apr 28, 2016 at 12:06

4 Answers 4

4

You have a typo. When you do *p dereferencing the pointer with gives you access to the underlying const int that you cannot change.

p = &y; 

On the other hand changes what p points to. Specifically it changes p to point to y which is legal.

int main() { const int x = 0; const int y = 30; const int *p = &x; std::cout << *p << "\n"; p = &y; std::cout << *p; return 0; } 

Output:

0 30 

Live Example

I also had to change

const int *p = x; 

to

const int *p = &x; 

otherwise you are trying initialize the pointer with the value of x and not the address of x.


You constexpr error has to do with the type of the pointer and what you are trying to point to.

constexpr int *w = &a; 

Says give me a int * and have it point to a and make this a constexpr. Now a is a const int not a int to trying to do that would remove the const of a which is illegal.

If we change it to

constexpr const int *w = &a; 

Then we have the right types but now we have a new error. a is not a constexpr so it cannot be used in a constexpr initialization as it is a local variable and will only have an address at runtime. If we make a static or a global variable then the address will be known at compile time and we can use it in a constexpr.

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

4 Comments

Oh Thanks a lot! So when you, assign a pointer, you dont need to have the astrix. Right now i dont know which anwser to accept. Because both of you have preety much the same code..
@Dsafds You can accept whichever answer you want. Pick the one that helps you the most and call it good. It would not upset me if you chose a different answer. It is your accept to give.
Indeed, pick what you think describes and answers your question best.
Nathan, i have edited my question. Im sorry for the multiple questions but i cant get constexpr's error...? I dont understand why i get the error.
3

To assign a pointer to some memory location of a variable you need to use the & address operator :

const int *p = &x; 

Same goes for the second statement:

*p = &y; 

You try to set the value of the variable p points to rather than the pointer itself.

Try :

p = &y; 

You shouldn't dereference to set the location of the pointer. The asterisk tells the compiler to return the value the pointer is pointing to, you don't want that.

3 Comments

Oh Thanks a lot! So when you, assign a pointer, you dont need to have the astrix. Right now i dont know which anwser to accept. Because both of you have preety much the same code..
Gill, i have edited my question. Im sorry for the multiple questions but i cant get constexpr's error...? I dont understand why i get the error.
@Dsafds: It's called an asterisk.
0

First of,

const int *p = nullptr; 

p is a pointer to int that points to a const int. This means that, p can be assigned to point to another address, however you can't change the object that p points via dereferencing p.

int* const p = nullptr; 

Here p is a const pointer to int. This means that you can change the object that p points by dereferencing p, however you can't change the adress that p points to.

On the other hand constexpr means a variable that can be used in a constant expression. A pointer declared constexpr is implicitly declared as a const pointer. That is,

constexpr int* p = nullptr; 

is with some differences equivalent with declaring:

int * const p = nullptr; 

One important difference is that you must initialize a constexpr pointer with a static initializer (e.g., address of global variable).

Comments

0

I am afraid you misunderstand the concept, first of all:

int i, j; const int *ptr = &i; *ptr = 123; // error ptr = &j; // ok 

makes ptr a non const pointer to const data, ie you cannot change variable ithrough this pointer but can change pointer itself. On another side:

int i, j; int * const ptr = &i; *ptr = 123; // ok ptr = &j; // error 

makes ptr constant pointer, so you can change value of i but you cannot change ptr itself, and:

int i, j; const int * const ptr = &i; *ptr = 123; // error ptr = &j; // error 

makes ptr const pointer to const data.

constexr on another side is completely different concept - it tells compiler that value has to be calculated at compile time and be constant. So that's why you cannot assign address of local variable to constexpr pointer.

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.