1

By the declaration

const int i;

it is clear that i cannot be modified.
Then why does the declaration

void f (const int *p) 

is modifying p? ( I tested it, it is modifying p but don't know how?).

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    possible duplicate of [What is the difference between char \* const and const char \*?](http://stackoverflow.com/questions/890535/what-is-the-difference-between-char-const-and-const-char) – Oliver Charlesworth Jul 15 '13 at 00:29
  • Again a wiki link: [Const Correctness](http://en.wikipedia.org/wiki/Const-correctness#Pointers_and_references) – Grijesh Chauhan Jul 15 '13 at 05:17
  • @GrijeshChauhan; The sentence **Thus `const` modifies the name to its right** is confusing on the wiki link(Second para, second line). – haccks Jul 15 '13 at 08:06
  • @haccks e.g in `const int *p` right of `const` is `*p`, In expression `int const * const constPtrToConst` right of first `const` after `int` is `*constPtrToConst` and right of second `const` is `constPtrToConst`. – Grijesh Chauhan Jul 15 '13 at 08:12
  • @GrijeshChauhan; How `const` modify name to its right? It should not. As in example your example right of second `const` is `constPtrToConst` and it will not modify. – haccks Jul 15 '13 at 08:40

3 Answers3

7

The placement of const in a pointer declaration matters. You read it backwards:

  • const int *p means "p is a pointer to int which is constant"
  • int * const p means "p is a constant pointer to int"
  • const int * const p means "p is a constant pointer to a constant int"
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

Because const is int pointed to by p, not p -- the pointer to the int.

const int *p

means that p is a pointer to const int, p can be modified, *p not.

The same way

int *const p

means that p cannot be modified, whereas *p can.

Note that const int* p is the same as int const* p.

The simple rule is that the declaration reads from right to the left: int const *p means "p is a pointer to constant int", int *const p means "p is a constant pointer to an int".

(The actual complete rule is more complicated, you can use http://cdecl.org/ for "decoding" C-style declarations.)

Vlad
  • 35,022
  • 6
  • 77
  • 199
3

because in this expression, p is a pointer to a const int. Which means you can change what p points to.

This also means that, "p" can itself be modified, but the contents of "p" cannot be modified, so *p = 10; will yield error.

An example will clear things:

#include <stdio.h>

void foo(const int *p)
{
  int x = 10;
  p = &x; //legally OK
  //  *p = 20; <- Results in an error, cannot assign to a read-only location. 
  printf("%d\n",*p);
}
int main(int argc, char *argv[])
{
  const int x10 = 100;
  foo(&x10);
  return 0;
}

To make the above program not modify the pointer at all:

#include <stdio.h>

void foo(const int *const p)
{
  int x = 10;
  p = &x; //legally NOT OK, ERROR!
  //  *p = 20; <- Results in an error, cannot assign to a read-only location. 
  printf("%d\n",*p);
}
int main(int argc, char *argv[])
{
  const int x10 = 100;
  foo(&x10);
  return 0;
}
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78