[cs13001] constant pointers

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Dec 12 15:48:31 EST 2012



> So i had a quick question. If you have a pointer to a constant, if you
> change where it points and deference it, what will the value be?
> 

A pointer to a constant is a pointer that cannot be used to change the
value in the memory location that the pointer points to. That is, you
can have the pointer point to different locations and dereference it,
just not change the value through the pointer.

for example:

  const int* p; // declares pointer to a constant
  int  j=1, k=2;

  p = &j;       // p points to memory location of j 
  cout << *p;   // prints out 1

  p = &k;
  cout << *p;  // prints out 2

  // *p = 3; changing through pointer to constant not allowed




Thanks,
--
Mikhail


More information about the cs13001 mailing list