devxlogo

An Easy Way to Remember Constant Pointer Declarations

An Easy Way to Remember Constant Pointer Declarations

Consider the following declarations:

int * const bla;int const * bla; const int * const bla;

As you can see, it can sometimes be hard to remember whether it’s the pointer that is declared constant or whether the pointer points to a constant variable or both.

The trick to deciphering these is to read the declaration backwards from right to left. For instance:

  • int * const bla; reads as: bla is constant and points to an int.
  • Specifically, bla is a constant pointer that can’t be changed and always points to the same int.

  • int const * bla; reads as: bla points to a const int. Specifically, bla can be changed but only points to integers that are const.
  • int const * const bla; reads as: bla is constant and points to a constant int. Specifically, bla is a constant pointer that can only point to a const int and neither can be changed.

Just remember: read backwards from right to left.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist