Const and Pointers.

Feb 29, 2012 at 2:41am
The questions are directed toward the second line in main(). I ran this as displayed and compared it to removing the const and reruning. No difference. Why? No impact with a variable change????

What is actually kept constant when using const in this example? If the results are the same, then const has no influence.

Thanks.

// Exercise to observe pointer and const combinations.

#include <iostream>

using namespace std;

int main ()
{
int Var = 18; // case 1.

const int * pVar = & Var;

cout << "pVar = " << pVar << endl;
cout << "*pVar = " << *pVar << endl;

Var = 20; // case 2.

cout << "pVar = " << pVar << endl;
cout << "*pVar = " << *pVar << endl;

system("pause");
return 0;
}
Feb 29, 2012 at 3:04am
What is actually kept constant when using const in this example?


The access path.

Given a modifiable object (Var), you may construct a const or a non-const access path (reference or pointer, possibly multilevel). The compiler will prevent you from using a const-qualified access path to modify the object, but it's perfectly OK to modify that object using a non-const access path.

Note that this is different from having a const object, e.g. const int Var. Doesn't matter if you have a pointer-to-const or pointer-to-nonconst, if it leads to the const object, modifying that object is undefined behavior.
Feb 29, 2012 at 3:16am
I don't understand your response.

Basically, the const is directed toward Var or &Var? If it is Var, then there should have been a compiler error when Var was reassigned. If it is &Var, then the results indicate const has no influence and therefore why include it?

Not sure how to relate your response to my question.

Thanks.
Feb 29, 2012 at 3:19am
Can anyone explain why next to my title is an icon of a person while everyone else has a new icon?
Feb 29, 2012 at 3:29am
The const is directed towards pVar. It is a pointer-to-const-int. It means, it's a pointer, through which one cannot modify the int.
Last edited on Feb 29, 2012 at 3:29am
Feb 29, 2012 at 3:35am
When you say "cannot modify the int.", the int means the type couldn't be change to float or char? Trying to understand what can't be modified.

Thanks.
Feb 29, 2012 at 3:53am
I mean the value of int, not its type. Types are always immutable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{

        int Var = 18;             // modifiable int

        const int * pcVar = &Var; // pointer to a non-modifiable int
        int * pmVar = &Var;       // pointer to a modifiable int
        const int& rcVar = Var;   // reference to a non-modifiable int
        int& rmVar = Var;          // reference to a modifibale int

//      *pcVar = 7; // illegal
        *pmVar = 7; // legal
//      rcVar = 7;  // illegal
        rmVar = 7;  // legal
}
Feb 29, 2012 at 4:25am
In my example, for:

const int * pVar = & Var;

does the syntax to the left of * mean that const int is applied to Var and Var is immutable?

Const to the right of * from my understanding means the const is applied to the pointer and the address must be constant?

If this is true, I don't understand why no compiler error when Var changed to 20?

Thanks.
Feb 29, 2012 at 8:39am
const int *

and

int const *

are the same. pointers to const int.

They mean that the value pointed to cannot be modified through the pointer. It does not mean the value pointed to is immutable.
Last edited on Feb 29, 2012 at 8:41am
Feb 29, 2012 at 9:05am
does the syntax to the left of * mean that const int is applied to Var and Var is immutable?
No, the type of 'Var' isn't changed. If you set Var=20; // Ok the dereference of 'pVar' will also change -> if((*pVar) == 20) // true
Topic archived. No new replies allowed.