2

I know that a pointer have an address and content cell which holds an address. So what happen to the pointer in the following code:

int a=5;
int* const ptr=&a;
*ptr=6;

The address ptr holds is not changed, so how can the value that ptr points to be changed?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
gbox
  • 809
  • 1
  • 8
  • 22
  • 3
    The same way a street address of a house doesn't change when it's sold, and new residents move in. Same house, different residents. – Sam Varshavchik Mar 24 '15 at 11:06
  • You actually wanted `const int* ptr=&a;` or maybe `const int* const ptr=&a;`. – Spikatrix Mar 24 '15 at 11:21
  • 1
    "The address ptr holds is not changed, so how can the value that ptr points to be changed" – exactly. they are orthogonal. The address which a pointer holds has nothing to do with the contents of the object it points to. – The Paramagnetic Croissant Mar 24 '15 at 11:23
  • so actually $const int* ptr$ can be changed by assign and address of another variable with other value? – gbox Mar 24 '15 at 11:53
  • 1
    in C, variable declarations need to be read from right to left by us humans, to clearly understand what the 'const' modifier(s) are saying is constant. so, 'int * const ptr = &a;' can be read as "ptr is a const * to int". which says the pointer is constant. reordering slightly: 'const int * ptr = &a;' can be read as "ptr is a pointer int const". which says ptr can be changed, but the int where it points cannot be change. finally,' const int * const ptr = &a;' can be read as "ptr is a const pointer to int const". I.E. neither the pointer nor the int being pointed at can be changed – user3629249 Mar 24 '15 at 11:58

6 Answers6

5
int *const ptr = &a;

Here ptr is a constant pointer so you can't modify the location to which this pointer points to. But you can change the value stored in the location the pointer is pointing to.

So

*ptr = 6;

will modify the value of the variable a to 6.

What is not allowed is along with the existing code say you have

int b=5;

and you do

ptr = &b;

Then you are bound to get a error saying the constant pointer is being made to point to some other memory location.

Gopi
  • 19,784
  • 4
  • 24
  • 36
4

If you go to a library and ask for a catalog then using the catalog find a book and then go and replace the book with a different one the catalog will still list the old book in it.

Similar thing happens in your code - the pointer (the reference itself) doesn't change, but what it points to does change.

If you meant to make the object that it points to be const then you need to declare it as such:

const int* ptr = &a;

or, to make both the object and the pointer const:

const int* const ptr = &a;
YePhIcK
  • 5,816
  • 2
  • 27
  • 52
0

There is a 4-Byte area in memory, let's say at address 0x2000, this area is called "a"

With the instruction a=5the compiler (or better startup-code) fills this memory area with the 4-Byte integer 0x00000005 (which is 5 in decimal).

now another variable ptr is filled with the address of a: ptr = 0x2000.

The pointer contains the address of memory area a we say ptr points to a

*ptr means: the memory area ptrpoints to, in this case memory at 0x2000.

So finally *ptr=6 means that the integer/value 6 is filled into the memory area ptr points to.

Now the memory area at 0x2000 will contain 0x00000006 (decimal 6)


Edit

The modifier const in int* const ptrmeans that the actual value i.e. the adress in ptr will never change during the execution of the program, it will always point to / contain 0x2000.

This means that an assignment like

ptr = &b;

will fail with a compiler error message.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
0

The value can change because what you are making constant is what the pointer points to, not what's inside of it.

Let me give you a way to think about it that is a good illustration but not meant to be literal. A pointer type could be viewed as an unsigned integer value (likely 32 or 64 bits in length, but that doesn't matter right now). In reality, all that's ever being stored into a pointer is an unsigned integer or Nil, which for our purposes we will call "zero".

When you define a pointer to be a constant in this way, you doing what this normally means; the content of this variable is constant. So the number that gets stuffed into this unsigned integer will not change.

However, when pointers are used (or dereferenced) they are special in that the value of the pointer is used to reference another memory location. That location is just another variable. Since your assignment does not change the actual pointer itself, it is completely valid. :)

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
0

Here the pointer ptr is of constant type. Constant pointer cannot change the address once it is loaded with an address.

In the third line *ptr=6; you are assigning the value to the address stored in the ptr pointer.ie.to a. So only the value of a changes to 6. To change the address of ptr remove the keyword const and assign a address like ptr=&b

Nerdy
  • 1,016
  • 2
  • 11
  • 27
0

Here pointer is a constant type, Using that pointer you can only point the single memory address. now you can't point the another address using this pointer

int a,c;
int *const b=&a;

and when we are assign like this, it is through the error

 b=&c;
Bhuvanesh
  • 1,269
  • 1
  • 15
  • 25