Intel Corporation interview question

Explain the difference between const int * and int const *.

Interview Answers

Anonymous

23 Dec 2015

The answer above is inaccurate. 1) const int * a1; 2) int const * a2; 3) int * const a3; 1 and 2 are the same ('const' keyword before the '*'): creates a pointer whose pointed data is constant, but the pointer value which contains the address itself is not constant. Yet 3 creates a pointer which is constant, but the values pointed to are variable. So: *a1 = 5; // error C2166: l-value specifies const object *a2 = 5; // error C2166: l-value specifies const object a2++; // works fine, pointer itself is not const and can be changed. a3++; // error C2166: l-value specifies const object *a3 = 5; // works fine, pointed value is not const and can be changed.

8

Anonymous

2 Nov 2015

const int * a; //creates a pointer whose pointed data is constant but the pointer value which contains the address itself is not constant int const * a; //creates pointer which is constant, but the values pointed to are varaible.

3