Solved

pointer to an object that cant be modified indirectly

Posted on 2004-03-23
6
197 Views
Last Modified: 2010-04-01
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
Comment
Question by:plumcrazy
  • 3
6 Comments
 
LVL 10

Expert Comment

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


Amit
0
 
LVL 10

Expert Comment

by:Sys_Prog
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

by:
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
read from right to left
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

by:dennis_george
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question