Skip to content
ajdevhub
1 min read

Pointers and const

Rule: ✅ const binds to left side; if none, binds to right.


1) Pointer to const (data const)

int a=10,b=20;
const int *p = &a;
  • can change pointer ✅
  • cannot change data through p ❌
p = &b;   // ✅
*p = 5;   // ❌

2) Const pointer (pointer const)

int * const p = &a;
  • cannot change pointer ❌
  • can change data ✅
*p = 5;  // ✅
p = &b;  // ❌

3) Const pointer to const data

const int * const p = &a;

Both cannot change.


Summary table

DeclarationChange pointer?Change data?
const int *p✅ yes❌ no
int *const p❌ no✅ yes
const int *const p❌ no❌ no

Reading trick (right to left)

  • int * const p → p is const pointer to int
  • const int * p → p is pointer to const int