Logo

What is the difference between const int*, const int * const, and int * const?

In C/C++, the placement of const relative to pointers—and how many times it appears—can be confusing. It helps to parse the declarations from right to left to see what exactly is being made constant. Below is a brief breakdown of each type:

  1. const int* (Pointer to a constant int)

    • Also seen as int const* (the position of const before or after int doesn’t change the meaning).
    • This means the integer value pointed to is constant—you cannot modify the int through this pointer.
    • However, the pointer itself is not constant. You can make the pointer point to another location if you want.
    • Example:
      const int* p = &x; // or int const* p = &x; *p = 10; // Error: can't modify the value p = &y; // OK: pointer can change where it points
  2. int * const (Constant pointer to a non-constant int)

    • The pointer itself is constant, meaning once initialized, it cannot point somewhere else.
    • The data pointed to is not constant—you can modify the int via this pointer.
    • Example:
      int * const p = &x; *p = 10; // OK: the value can be modified p = &y; // Error: can't change the pointer itself
  3. const int * const (Constant pointer to a constant int)

    • Both the pointer itself is constant (it cannot point to another address),
    • and the data it points to is also constant (the int cannot be modified through this pointer).
    • Example:
      const int * const p = &x; // or int const * const p = &x; *p = 10; // Error: can't modify the value p = &y; // Error: can't change the pointer's address

Quick Reference

  • const int* (or int const*): You can change the pointer, not the pointed value.
  • int * const: You can change the pointed value, not the pointer address.
  • const int * const: You can neither change the pointer address nor the pointed value.

Further Learning

Understanding pointers and memory management is critical for tackling complex data structures and algorithms. If you want to dive deeper into these fundamentals and prepare for coding interviews, here are two highly recommended courses from DesignGurus.io:

  1. Grokking Data Structures & Algorithms for Coding Interviews
    Strengthen your grasp of arrays, linked lists, trees, graphs, and more—complemented by practical coding exercises.

  2. Grokking the Coding Interview: Patterns for Coding Questions
    Learn the recurring coding patterns behind common interview questions, so you can solve problems quickly and with confidence.

Both courses will reinforce critical topics related to pointers, memory, and algorithmic thinking—equipping you with the knowledge to excel in real-world coding tasks and technical interviews.

CONTRIBUTOR
TechGrind