Machine-translated by Google. Questions are authored in English.
Top

Discussion

Comment on the following.

const int *ptr;

  • A.You cannot change the value pointed by ptr
  • B.You cannot change the pointer ptr itself
  • C.You May or maynot change the value pointed by ptr
  • D.You can change the pointer as well as the value pointed by it

Answer: A

Correct answer: You cannot change the value pointed by ptr

The const applies to what ptr points at, not to ptr itself, so *ptr = 5 is rejected while ptr = &other is allowed.

Read such declarations from the identifier outwards, or apply the rule that const qualifies whatever lies to its left unless there is nothing there, in which case it qualifies what lies to its right. int * const ptr reverses the meaning - a constant pointer to a modifiable int - and const int * const ptr fixes both.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.