flawr
flawr

Reputation: 11628

Why a constant integer pointer point to a non constant integer allowed?

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?

Upvotes: 1

Views: 103

Answers (4)

CiaPan
CiaPan

Reputation: 9570

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.

Upvotes: 0

lokippc
lokippc

Reputation: 13

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

Upvotes: -4

eerorika
eerorika

Reputation: 238321

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.

Upvotes: 3

songyuanyao
songyuanyao

Reputation: 172924

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

Upvotes: 4

Related Questions