Solved

# pointer to an object that cant be modified indirectly

Posted on 2004-03-23
196 Views
How would you define a pointer P that is initialized to point to the location of an int object i . The definition should make it illegal to modify the value of i indirectly through P .
0
Question by:plumcrazy
• 3

LVL 10

Expert Comment

ID: 10664287
int main()
{
int i = 10 ;
const int *p = &i ;
*p = 20 ;
system ( "PAUSE" ) ;
return 0;
}

Amit
0

LVL 10

Expert Comment

ID: 10664305
The line '*p = 20' in the above shown code gives an error saying a 'assignment to a const location'

The following code demostrates that u can't change i indirectyly thru P, however u can change i directly

int main()
{
int i = 10 ;
const int *p = &i ;
cout << "i : " << i <<endl ;
cout << "*p : " << *p <<endl;

*p = 20 ;                                    // Syntax Error (Comment this for the program to run )
i = 30 ;
cout << "i : " << i <<endl;
cout << "*p : " << *p <<endl;
system ( "PAUSE" ) ;
return 0;
}

Amit
0

LVL 10

Accepted Solution

Sys_Prog earned 125 total points
ID: 10664351
There are two important thing regarding using const with pointers

Constant Pointer - A pointer which cannot point to some other location after being initialized to point to some location. However     the  value at which the pointer points can be changed

int * const p = &i ;

Pointer to const - A pointer which can be made to point to some other location. However the value pointed to by the pointer cannot be changed

int const *p = &i ;
const int *p = &i ;

It's easy to distinguish/write the two
Thus, for first case above, "p is a const pointer to a integer"
and for second case above, "p is a pointer to a const integer" OR "p is a pointer to a integer const"

You can combine the above both to have a const pointer to a const integer

HTH

Amit

0

LVL 5

Expert Comment

ID: 10664685
Hi,

I think if you want that nobody can change the value of int directly then declare it as

const int i = 10 ;

By this way even if you assign the address of int to a non constant pointer you will get a warning or error. (because you are advised to assign address of a const data type to a non const pointer) :: by const pointer I mean to say pointer to a constant location.

But if you only want that the pointer you declare should not change its value then Sys_prog is quite precise in his answer.

Dennis
0

## Featured Post

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.