Question discussion

Comment on the following?const int *ptr;

297361Medium 1 reply 960 views
29
Comment on the following?const int *ptr;
  • AWe cannot change the value pointed by ptr.
  • BWe cannot change the pointer ptr itself.
  • CBoth of the above
  • DWe can change the pointer as well as the value pointed by it.
Correct answer: A

Discussion 1

0
CCommunity
We cannot change the value pointed by ptr.

int * : pointer to int
int const * : pointer to const int
int * const : const pointer to int
int const * const : const pointer to const int

Now the first const can be on either side of the type so:

const int * == int const *
const int * const == int const * const

So the above declaration is pointer to const int. Which means,we cannot change the value pointed by ptr.