VOOZH about

URL: https://www.geeksforgeeks.org/c/output-of-the-program-pointer-to-a-constant-or-constant-pointer/

⇱ Output of the Program | Pointer to a Constant or Constant Pointer? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Output of the Program | Pointer to a Constant or Constant Pointer?

Last Updated : 21 Jun, 2022

Predict the output of the below program.
 


Output
6


Explanation: 
See following declarations to know the difference between constant pointer and a pointer to a constant. 
int * const ptr ---> ptr is constant pointer. You can change the value at the location pointed by pointer p, but you can not change p to point to other location.
int const * ptr ---> ptr is a pointer to a constant. You can change ptr to point other variable. But you cannot change the value pointed by ptr.
Therefore above program works well because we have a constant pointer and we are not changing ptr to point to any other location. We are only incrementing value pointed by ptr.
Try below program, you will get compiler error. 
 

Comment
Article Tags:
Article Tags: