Click here to Skip to main content
15,886,075 members
Articles / Programming Languages / C++
Tip/Trick

Understanding C/C++ Type Declaration

Rate me:
Please Sign up or sign in to vote.
3.33/5 (9 votes)
28 Jan 2017CPOL 13.6K   5   3
C/C++ type declaration

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).

C++
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.

C++
int const * np;

np is a pointer to constant integer, meaning the dereferenced value is constant and cannot be modified.

C++
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.

C++
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.

C++
int *& np;

np is a reference to pointer to integer.

C++
int* arr[10];

arr is an array of pointers to integer.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
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.

Comments and Discussions

 
QuestionRefile this as a tip/trick Pin
David O'Neil29-Jan-17 10:08
professionalDavid O'Neil29-Jan-17 10:08 
AnswerRe: Refile this as a tip/trick Pin
Shao Voon Wong29-Jan-17 17:05
mvaShao Voon Wong29-Jan-17 17:05 
GeneralRe: Refile this as a tip/trick Pin
David O'Neil29-Jan-17 17:45
professionalDavid O'Neil29-Jan-17 17:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.