Understanding C/C++ Type Declaration
The trick to decipher the C++ types declaration is to 'read from right to left'! I learnt this from an old book titled Visual C++ 5 Bible which is published in 1997 (now 20 years old).
const int * np;
np
is a pointer to integer constant, meaning the dereferenced value is constant and cannot be modified. Below is another equivalent declaration. It does not matter whether const
appears on the left or right side of the data type.
int const * np;
np
is a pointer to constant integer, meaning the dereferenced value is constant and cannot be modified.
int * const np;
np
is a constant pointer to integer, meaning the pointer is constant and cannot be modified but dereferenced value can be modified.
int const * const np;
np
is a constant pointer to constant integer, meaning the pointer and dereferenced value are both constant and cannot be modified.
int *& np;
np
is a reference to pointer to integer.
int* arr[10];
arr
is an array of pointers to integer.
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.
In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed
here.