Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Nov 6, 2007

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.

Jacob Lund Fisker
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date