C const and Pointers

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is const and Pointers?

With pointers, const can protect the data, the pointer, or both. Read the declaration right to left: const int *p is a pointer to const int (data is read-only); int *const p is a const pointer to int (pointer cannot move); const int *const p makes both read-only.

Remember
const int *p;          /* data const, pointer can move     */
int *const p = &x;     /* pointer const, data can change   */
const int *const p = &x;   /* both const                   */

Cheat Sheet

Keep these facts handy while you work with const and Pointers in C.

TopicDetail
Pointer to constCannot write *p
Const pointerCannot reassign p
BothNeither can change
ParametersUse const T * for read-only inputs

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Structures, Unions and Enums.

Example 1 — Three const pointer forms

Each form allows a different change.

C
#include <stdio.h>

int main(void) {
    int a = 1, b = 2;

    const int *p1 = &a;       /* data const */
    p1 = &b;                  /* ok: pointer moves */

    int *const p2 = &a;       /* pointer const */
    *p2 = 10;                 /* ok: data changes */

    const int *const p3 = &a; /* both const */

    printf("*p1=%d *p2=%d *p3=%d a=%d\n", *p1, *p2, *p3, a);
    return 0;
}

How It Works

  1. p1 may point somewhere else, but *p1 = 5 would not compile.
  2. p2 always points to a, but can change a.
  3. p3 can neither move nor write; it just reads a, which is now 10.

Edge Cases & Pitfalls

Check these before relying on const and Pointers in real programs.

cast

Casting const away

Writing through a pointer cast from const to non-const is undefined if the object is really const.

order

Reading the declaration

const int *p and int const *p mean the same; int *const p is different.

Key Takeaways

  • Right to left: read declarations backwards.
  • API: const T * documents read-only input.
  • Safe: const helps the compiler catch bugs.

One line: With pointers, const can protect the data, the pointer, or both.

Frequently Asked Questions

const int *p is a pointer to constant data: you cannot modify *p but you can reassign p. int *const p is a constant pointer: you can modify *p but cannot reassign p.
It tells callers the function will not modify the data and lets the compiler catch accidental writes.
Continue with "C Structures, Unions and Enums" next. Build your own types with structures, unions, enumerations, typedef, and bit-fields. The C Pointers hub lists every topic in order.

Next: Structures, Unions and Enums

Build your own types with structures, unions, enumerations, typedef, and bit-fields.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful