1

I'm currently trying to learn some C++ and came across following unintuitive behavior. As t is a pointer to a const int I would expect *t to stay the same as long as we do not change t.

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    int a = 3;
    const int* t = &a; //Why is this allowed? a is NOT const int
    a = 4;
    cout << *t << endl; //This does then print 4
    //*t = 4; //Throws error.

    return 0;
}

Can anyone explain why this is does compile?

6
  • to clear the confusion i would suggest to start without pointers. int a = 1; const int b = a; is just fine, and now b is const while a is not, even if the hold the same value Commented Apr 8, 2016 at 9:18
  • @tobi303 Yes, but in your example b receives a copy of the value of a. Commented Apr 8, 2016 at 9:19
  • 1
    well, then what about int a = 1; const int& b = a; now b is a reference for b, but you cannot change the value through b even if a is not const Commented Apr 8, 2016 at 9:22
  • @tobi303 Ok, originally would not have expected this to work as well. But now I get the idea of the responsibilty as someone called it below, so that makes sense, thank you for your help! Commented Apr 8, 2016 at 9:24
  • pass by value is usually done via void foo(const T&) this just means, that inside the function, the value cannot change, but the parameter passed to the function is not necessarily const Commented Apr 8, 2016 at 9:30

4 Answers 4

4

const int* t just means you can't change the value t pointing to by t, nothing more. The original value might be changed, but it has nothing to do with t's responsibility.

If you want to ensure the value won't be changed, you should let t point to a const, such as

const int a = 3;
const int* t = &a;

And for this case, you can't make a int* pointer point to it.

int* t = &a; // error
Sign up to request clarification or add additional context in comments.

2 Comments

That makes sense. I expected that const int* can only accept adresses to const int!
@flawr int* could be implicitly casted to const int*, it just means you won't change the value via it. For example you could pass a int* to function, if you won't change the value inside the function, you can make the function takes const int* as the parameter.
3

As t is a pointer to a const int I would expect *t to stay the same as long as we do not change t.

You cannot make that assumption in general, because t might point to a non-const object, such as in your example.

const int* t = &a; //Why is this allowed? a is NOT const int

Can anyone explain why this is does compile?

The rules of c++ allow the implicit conversion of T* to const T*. It is allowed, because it's very useful to have a pointer-to-const (or reference) to an non-const object. Pointer to const simply means that the object cannot be modified "through" the pointer. The object itself could be const, or non-const.

As an example of why it is useful, you could have some modifiable state as a private member of an object, and return const view to it so that others can observe, but not modify. A practical example of such is std::string::c_str(). It returns a const char* even though the internal buffer of the std::string is non-const.

1 Comment

Thank you for this explanation !t makes sense now when thinking of the type as what you can do through that exacty variable.
0

As t is a pointer to a const int I would expect *t to stay the same as long as we do not change t.

The answer is simple: the const keyword in a pointer type declaration does not mean 'it is constant' but rather 'you are not allowed to modify it'.

This is useful for example when you create a variable and then call another function to do something with it, but you want to forbid modification:

    extern int countZerosInArray( const int *array, int arrayLen);

    int myVariableArray[ 100 ];
    // ... fill the array - i.e. modify it!
    int noOfZeros = countZerosInArray(myVariableArray, 100);

The countZerosInArray function is told it has read-only access to the array, although the array itself of course is not constant.

Comments

-4

const int* t = &a; // int* or const int* can assgin to const int*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.