Sunday, September 08, 2013

C++ declarations



consider C++ declaration

const int *kings[];  
what is it ? is it an array of pointers to ints or pointer to array of ints ?

The Rule is simple:
  • There are two types of operators in the declarations prefix and postfix. 
  • postfix operators bind tighter than prefix ones.
  • thus const int *kings[]  is array of pointers. where as to declare pointer to array you have to use brackets.
  • const int (* kings)[]; // now we have increased precedence of * by brackets and hence it is pointer to array of ints
const int *kings[]; //array of pointers of ints .
const int (*kings)[]; //pointer to array of ints

similarly

const int (*kings)(); //pointer to a function taking no params and returning const int.

be aware where the brackets go 

const (int *) kings[] /// is WRONG.